Java根据输入年月日输入计算位于某年中的第几天 今天药忘吃喽~ 2021-06-22 15:38 541阅读 0赞 > 要求: > > 键盘输入年、月、日,判断这是那一年的第几天? 判断闰年标准: 1)可以被4整除,但不能被100整除 2)可以被400整除 import java.util.Scanner; class Test4{ public static void main(String[] args){ //实例化对象 Scanner input = new Scanner(System.in); System.out.println("请输入year"); int year = input.nextInt(); System.out.println("请输入month"); int month = input.nextInt(); System.out.println("请输入day"); int day = input.nextInt(); //记录总天数 int sumDay = 0; switch(month){ case 12: sumDay += 30; case 11: sumDay += 31; case 10: sumDay += 30; case 9: sumDay += 31; case 8: sumDay += 31; case 7: sumDay += 30; case 6: sumDay += 31; case 5: sumDay += 30; case 4: sumDay += 31; case 3: // 判断是否是闰年 if((year % 4 == 0 && year % 100 !=0) || year % 400 == 0){ sumDay += 29; } else{ sumDay += 28; } case 2: sumDay += 31; case 1: sumDay += day; } System.out.println(year + "年" + month + "月" + day + "日是" + year + "年的第" + sumDay + "天"); } }
还没有评论,来说两句吧...