/* 制作万历表 分析: 1、获取年份和月份的数据 2、获取上个月到1900年1月1日的总天数 3、然后除以7取余数,余数就是1号前的空格数 */ import java.util.Scanner; public class Exercise8 { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("请输入一个年份:"); int a = input.nextInt(); System.out.print("请输入一个月份:"); int b = input.nextInt(); int c = 0; //计算总天数 int d1 = 0; //计算 for (int i = 1900; i < a; i++) {//判断年份 if (i%4 == 0 && i%100 !=0 || i%400 == 0){ c = c+366; }else c = c+365; } for (int i = 1; i <= b; i++) {//判断月份 switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: c += 31; break; case 2: if (a%4 == 0 && a%100 !=0 || a%400 == 0){ c += 29; break; }else{ c += 28; break; } case 4: case 6: case 9: case 11: c +=30; break; } } int dum = sele(a,b); int c1 = c - dum; System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日\t"); for (int i = 0; i < c1%7; i++) { System.out.print(" \t\t"); d1++; } for (int i = 1; i <=dum ; i++) { System.out.print(i+"\t\t"); d1++; if (d1%7 == 0) System.out.println(); } System.out.println(); System.out.println(c); } public static int sele(int a,int b){ int d = 0; switch(b){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: d = 31; break; case 2: if (a%4 == 0 && a%100 !=0 || a%400 == 0){ d = 29; break; }else{ d = 28; break; } case 4: case 6: case 9: case 11: d = 30; break; } return d; } }
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交