package studying; import java.util.Scanner; public class JudgmentDate { /* * Title: Enter a certain day of the year, to determine the day of the year. */ public static void main(String[] args) { int year; int month; int day = 0; int days; //Cumulative days int d = 0; int e = 0; Scanner input = new Scanner(System.in); do { System.out.println("Please enter year:"); year = input.nextInt(); System.out.println("Please enter month:"); month = input.nextInt(); System.out.println("Please enter day:"); days = input.nextInt(); if(month < 0 || month > 12 || days > 31) { System.out.println("Input error!"); e = 1; } }while( e == 1); for(int i = 1; i < month; i++) { switch(i) { case 1:case 3:case 5:case 7:case 8:case 10:case 12:{ day = 31; break; } case 4:case 6:case 9:case 11:{ day = 30; break; } case 2:{ /* * 闰年:①非整白年数除以4,无余数为闰年,有余数为平年; * ②整百年数除以400,无余数。 * 二月:平年28天,闰年29天。 */ if((year % 100 != 0 && year % 400 == 0) ||(year % 100 == 0) && (year % 400 == 0)) { day = 29; }else { day = 28; } } default:break; } d += day; } System.out.println("This is the " + (d +days) + "day of " + year); } }
结果截图: