/** * 编写程序,计算2013年的某月某日是当年的第多少天?是星期几?已知2013年元旦是星期二。 */ import java.util.Scanner; public class Date { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入年:"); int year=input.nextInt(); System.out.println("请输入月:"); int month=input.nextInt(); System.out.println("请输入日:"); int day=input.nextInt(); int days=0; int monthDay=0; int allDay=0; if (year <= 0 || month <= 0 || month > 12 || day <= 0 || day > 31) { System.out.println("输入错误,请重新输入!"); }else{ if(month==1){ allDay=day; } else{ for (int i = 1; i < month; i ++) { switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { days = 29; } else{ days = 28; } break; } monthDay =monthDay+days; } } allDay=monthDay+day; System.out.println(year +"年 "+month+"月"+day+"日是这一年的第" +allDay+"天。"); int x=(allDay+1)%7; if(x==0){ System.out.println(year +"年" +month+ "月"+ day+ "日是这一年的星期天" ); }else { System.out.println(year +"年" +month+ "月"+ day+ "日是这一年的星期"+x ); } } } }
时间: 2024-10-10 09:15:30