从键盘分别输入年、月、日,判断这一天是当年的第几天。
备注:本次假设用户输入的日期均为合法日期。
具体代码实现如下:
import java.util.Scanner;
public class TestDayOfYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("年份:");
int year = sc.nextInt();
System.out.print("月份:");
int month = sc.nextInt();
System.out.print("天数:");
int day = sc.nextInt();
int tianShu = day;
switch(month){
case 12:
tianShu += 30;
case 11:
tianShu += 31;
case 10:
tianShu += 30;
case 9:
tianShu += 31;
case 8:
tianShu += 31;
case 7:
tianShu +=30;
case 6:
tianShu += 31;
case 5:
tianShu += 30;
case 4:
tianShu += 31;
case 3:
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){// 判断是否为闰年
tianShu += 29;
}else{
tianShu += 28;
}
case 2:
tianShu += 31;
}
System.out.println(year + "年" + month + "月" + day + "日" + "," + "是" + year + "的第" + tianShu +"天");
}
}
人生若只如初见,何事秋风悲画扇。
等闲变却故人心,却道故人心易变。
-----------纳兰性德
小白寄语:学如逆水行舟,不进则退。