如何打使用myeclipse打印日历,求解
具体要 用户输入年份 然后打印出这一年的日历 帮帮忙……
------解决方案--------------------
做个循环就行了,这是main方法中加上循环以后的代码,楼主参考一下吧
- Java code
Scanner sc = new Scanner(System.in); System.out.println("请输入年份:"); int year = sc.nextInt(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); for (int month = 1; month < 13; month++) { System.out.println("\n" + month + " 月:"); int days = 0; cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, 1); int week = cal.get(Calendar.DAY_OF_WEEK); int spaceNum = week - 1; switch (month) { 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 % 4 == 0 && year % 100 != 0) || year % 400 == 0) { days = 29; } else { days = 28; } break; } System.out.println("日\t一\t二\t三\t四\t五\t六"); for (int i = 1; i <= days + spaceNum; i++) { if (i <= spaceNum) { System.out.print("\t"); } else { System.out.print(i - spaceNum + "\t"); } if (i % 7 == 0) { System.out.println(); } } }
------解决方案--------------------
利用month进行一个for循环应该就可以了吧!
- Java code
for (month = 1; month <= 12; month++) { cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_WEEK, days); int week = cal.get(Calendar.DAY_OF_WEEK); int spaceNum = week - 1; System.out.println("------------------" + month + "月------------------");