1、首先新建类weekTest,新建完成后,写入下面的代码。package zhousi34jie;import java.util.Scanner;
2、定义一个方法,判断是否是闰年public static boolean isLeap(int year){ boolean leap=year%4==0||year%100!=0&&year%400==0;//判断是否为闰年 return leap; }
3、下面是main方法中的内容。
4、思路:定义a,b记录闰年和平年个数,因为一个366天,一个365天。然后使用Scanner类使用户输入自己的日期。int a=0,b=0;//a为闰年个数,b为平年个数 Scanner sc=new Scanner(System.in); int year=sc.nextInt(); int month=sc.nextInt(); int day=sc.nextInt();
5、//计算有多少个闰年和多少个平年。从1980年1月1日星期二开始 for(int i=1980挢旗扦渌;i<year;i敫苻匈酃++){ if(isLeap(i)) { System.out.printf("%d是闰年%n",i); a++; } else{ System.out.printf("%d是平年%n",i); b++; } } System.out.printf("闰年有%d个,",a); System.out.printf("平年有%d个",b); int tian=a*2+b*1;
6、思路:计算从输入年份的1月到输入的month月一共有多少天i艘绒庳焰nt yue=0; for(int j=month-1;j>稆糨孝汶;0;j--){ switch(j){ case 1:case 3:case 5:case 7:case 8:case 10:yue+=31;break; case 4:case 6:case 9:case 11:yue+=30;break; case 2:yue+=isLeap(year)?29:28; } } System.out.printf("从"+year+"年1月到"+year+"年"+month+"月一共有%d天%n",yue);
7、思路:计算总天数int total=tian+yue+day; System.out.printf("总天数=%d天%n", total);
8、思路:计算星期几week=(week+total)%7;Sy衡痕贤伎stem.out.printf(year+"年"+month+"月"+day+"日 星期"); switch(week) { case 0:System.out.println("日");break; case 1:System.out.println("一");break; case 2:System.out.println("二");break; case 3:System.out.println("三");break; case 4:System.out.println("四");break; case 5:System.out.println("五");break; case 6:System.out.println("六");break; }