编写程序:从键盘上输入任意年份year年的任意月分month和任意日,要求通过程序输出是该年的第几天。

199 阅读1分钟
分析:首先,要有键盘扫描器scanner接收所输入的年,月,日。月分有十二个月可用switch语句。
     然后,题目中的年和月都有特殊的部分。年有平年(365天)和闰年(366天),
     月份有大月(31天),小月(30天),还有最特殊的二月(2829天)都可用判断if(){}erse{}来搞定。
     最后,平闰年的区分和2月份2829天的区分是相同的,有个通式:{ 年的天数%400==0||(年的天数%4==0 && 年的天数%100!=0) }
     想要求出某年某月,天数的总和只需相加其对应的天数便可。
int year=0;
int month = 0;              //定义三个空盒子,用来存输入值对应的天数
int day = 0;

Scanner scanner = new Scanner(System.in);
System.out.println("请输入任意年:");
int input = scanner.nextInt();
if (input%400==0||(input%4==0&&input%100!=0)){
    System.out.println((year=366)+"天");
}else {
    System.out.println((year=365)+"天");
}

System.out.println("任意月分");
int input1 = scanner.nextInt();
switch (input1){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        System.out.println((month=31)+"天");
        break;
    case 2:if (input%400==0||(input%4==0&&input%100!=0))//二月判别器
    {
        System.out.println((month=29)+"天");

    }else {
        System.out.println((month=28)+"天");
    }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        System.out.println((month=30)+"天");
        break;
}

System.out.println("任意日");
int input2 = scanner.nextInt();
System.out.println(input2);
System.out.println("是"+input+"年"+"的第:"+(year+month +input2)+"天");  //使用三个盒子中的数字相加