输入一个人的出生日期(包括年月日),将该生日中的年、月、日分别输出。
数据范围:年份满足 ,月份满足 ,日满足
输入只有一行,出生日期,包括年月日,年月日之间的数字没有分隔符。
三行,第一行为出生年份,第二行为出生月份,第三行为出生日期。输出时如果月份或天数为1位数,需要在1位数前面补0。
20130225
year=2013 month=02 date=25
通过scanf函数的%m格式控制可以指定输入域宽,输入数据域宽(列数),按此宽度截取所需数据;通过printf函数的%0格式控制符,输出数值时指定左面不使用的空位置自动填0。
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String[] args) throws ParseException { Scanner in = new Scanner(System.in); Calendar calendar = Calendar.getInstance(); calendar.setTime(new SimpleDateFormat("yyyyMMdd").parse(in.nextLine())); System.out.println("year="+calendar.get(Calendar.YEAR)); int month = calendar.get(Calendar.MONTH)+1; String m = month < 10 ? "0"+ month : "" + month; System.out.println("month="+ m); int date = calendar.get(Calendar.DATE); String d = date < 10 ? "0" + date : "" + date; System.out.println("date="+d); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String inputString = in.nextLine(); String year = inputString.substring(0, 4); String month=inputString.substring(4,6); String divide=inputString.substring(6,8); System.out.println("year="+year); System.out.println("month="+month); System.out.println("date="+divide); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); System.out.println("year=" + str.substring(0,4)); System.out.println("month=" + str.substring(4,6)); System.out.println("date=" + str.substring(6,8)); } }
import java.util.*; public class Main { public static void main(String [] args) { Scanner input=new Scanner(System.in); String result=input.next(); System.out.println("year="+result.substring(0,4)); System.out.println("month="+result.substring(4,6)); System.out.println("date="+result.substring(6,8)); } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sin = new Scanner(System.in); String str = sin.nextLine(); System.out.println("year=" + str.substring(0,4) + "\n" + "month=" + str.substring(4,6) + "\n" + "date=" + str.substring(6,8)); } } 三行代码搞定,简单高效
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String people = in.nextLine(); //substring()方法:返回字符串在范围内的子段 System.out.println("year="+people.substring(0, 4)); System.out.println("month="+people.substring(4,6)); System.out.println("date="+people.substring(6, 8)); } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String input = scanner.next(); String year = input.substring(0,4); String month = input.substring(4,6); String date = input.substring(6,8); System.out.println("year="+year); System.out.println("month="+month); System.out.println("date="+date); } }String的substring题解😄
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);//输入 int data = scanner.nextInt();//输入整数 int year = data / 10000; //直接整除得到年份 int Month = (data % 10000)/100; //求余得到年份 int day = (data % 100) ; //求余得到日期 /** * year=2013 * month=02 * date=25 */ System.out.println("year="+year); System.out.printf("month=%02d\n", Month); System.out.printf("date=%02d\n", day); } }我的解题思路不一定是最优解,我看到楼上也有用子串的方式,我回去试试看哪个占用内存时间少
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String read = bf.readLine(); System.out.println("year="+read.substring(0,4)); System.out.println("month="+read.substring(4,6)); System.out.println("date="+read.substring(6,8)); } }
字符串的截取
import java.util.*; public class Main { public static void main(String [] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()) { String str=sc.next(); System.out.println("year="+str.substring(0,4)); System.out.println("month="+str.substring(4,6)); System.out.println("date="+str.substring(6,8)); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String year = s.substring(0, 4); String month = s.substring(4, 6); String days = s.substring(6, 8); System.out.println("year="+year); System.out.println("month="+month); System.out.println("date="+days); } }