需求:

  • 编写程序,对输入的年、月、日,给出该天是该年的第多少天?
Scanner input = new Scanner(System.in);
System.out.println("分别输入年月份:");
System.out.print("输入年份:");
int year = input.nextInt();
System.out.print("输入月份:");
int month = input.nextInt();
System.out.print("输入本月日期:");
int data = input.nextInt();
int sum = 0;

for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum = sum +31;
continue;
case 4:
case 6:
case 9:
case 11:
sum = sum + 30;
continue;
}
}
if (year%4==0&&month>2) {
sum = sum +29;
}else if (month>2){
sum = sum +28;
}
sum = sum + data;
System.out.println("今天是"+year+"年的第"+sum+"天");

实现效果图,如:

java实现输入年月日,判断为该年的第几天(采用for、witch方法)_java