输入年月日查询是这一年的第几天
运行结果
代码
//导包 import java.util.Scanner; //声明一个类名为Test的类 public class Test { //mian方法 程序入口 public static void main(String[] args) { // 输入年月日查询是这一年的第几天 //键盘输入 Scanner scan = new Scanner(System.in); System.out.println("******输入年月日查询是这一年的第几天******"); //定义二月的天数 int febDay = 0; //定义总天数 int sumDays = 0; System.out.print("请您输入年份:"); //获取键盘输入值---获取年份 int year = scan.nextInt(); System.out.print("请您输入月份:"); //获取键盘输入值---获取月份 int month = scan.nextInt(); System.out.print("请您输入日:"); //获取键盘输入值---获取日 int day = scan.nextInt(); //判断年月日是否符合条件 符合执行计算操作 否则输出错误 if (year >= 0 && month > 0 && month < 13 && day > 0 && day < 32) { //判断年份是否是闰年 是闰年二月29天 否则二月28天 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { febDay = 29; } else { febDay = 28; } //判断选择月份 switch (month) { //月份为12时 case 12: //计算总天数 sumDays = 31 * 6 + 30 * 4 + febDay + day; //停止循环 break; case 11: sumDays = 31 * 6 + 30 * 3 + febDay + day; break; case 10: sumDays = 31 * 5 + 30 * 3 + febDay + day; break; case 9: sumDays = 31 * 5 + 30 * 2 + febDay + day; break; case 8: sumDays = 31 * 4 + 30 * 2 + febDay + day; break; case 7: sumDays = 31 * 3 + 30 * 2 + febDay + day; break; case 6: sumDays = 31 * 3 + 30 + febDay + day; break; case 5: sumDays = 31 * 2 + 30 + febDay + day; break; case 4: sumDays = 31 * 2 + febDay + day; break; case 3: sumDays = 31 + febDay + day; break; case 2: sumDays = 31 + day; break; case 1: sumDays = day; break; } // 输出 System.out.println("您输入年月日是这一年的第" + sumDays + "天"); }else{ // 输出错误信息 System.out.println("输入有误!"); } } }