判断一年中第几天
输入某年某月某日,判断这一天是这一年的第几天?
import java.util.*; public class lianxi09{ public static void main(String[] args){ int year = 0, month = 0, day = 0; int run = 0, d = 0; int[] monthday = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Scanner sc = new Scanner(System.in); while(true){ System.out.println("请输入年:"); year = sc.nextInt(); System.out.println("请输入月:"); month = sc.nextInt(); System.out.println("请输入天:"); day = sc.nextInt(); if(year < 0 || month <= 0 || month > 12 || day <=0 || day > 31) continue; break; } if((year % 400) == 0 || (year % 4 == 0 && year % 100 != 0)){ run = 1; } monthday[2] = monthday[2] + run; for(int i = 0; i < month; i++){ d += monthday[i]; } System.out.println(year+"年"+month+"月"+day+"日是这一年的第"+(d + day)+"天"); } }