( 显示日历)编写程序,提示用户输人年份和代表该年第一天是星期几的数字,然后在控制台上
显示该年的日历表。例如,如果用户输人年份 2013 和代表 2013 年 1 月 1 日为星期二的 2, 程
序应该显示该年每个月的日历,如下所示:
package javaBasicPart;
import java.util.Scanner;
public class showCalendar {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the year:");
int year= input.nextInt();
System.out.println("Please enter the first day of the year:(0 means Sunday)");
int firstDay= input.nextInt();
String[] month={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
for (int i=0;i<12;i++){
int count = 0;
System.out.println("n");
System.out.println("ttttt" + month[i] + " " + year);
System.out.println("-----------------------------------------------------");
System.out.println(" Sunt "+" Mont " + " Tuet " + " Wedt " + " Thut " + " Frit " + " Sat ");
if (firstDay == 7) {
System.out.print(" ");
}
else if (firstDay == 1) {
System.out.print(" t t");
count += 2;
}
else if (firstDay == 2) {
System.out.print(" t t t t");
count += 4;
}
else if (firstDay == 3) {
System.out.print(" t t t t t t");
count += 6;
}
else if (firstDay == 4) {
System.out.print(" t t t t t t t t");
count += 8;
}
else if (firstDay == 5) {
System.out.print(" t t t t t t t t t t");
count += 10;
}
else if (firstDay == 6) {
System.out.print(" t t t t t t t t t t t t");
count += 12;
}
if (i == 1) {
if (isLeap(year)) {
for (int k = 1; k <= 29; k++) {
if (count % 14 == 0&&count!=0)
System.out.println(" ");
System.out.print("t"+k + " t");
count+=2;
}
firstDay=(firstDay+29)%7;
} else {
for (int k = 1; k <= 28; k++) {
if (count % 14 == 0&&count!=0)
System.out.println(" ");
System.out.print("t"+k + " t");
count+=2;
}
firstDay=(firstDay+28)%7;
}
}
if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) {
for (int k = 1; k <= 31; k++) {
if (count % 14 == 0&&count!=0)
System.out.println(" ");
System.out.print("t"+k + " t");
count+=2;
}
firstDay=(firstDay+31)%7;
}
if (i == 3 || i == 5 || i == 8 || i == 10) {
for (int k = 1; k <= 30; k++) {
if (count % 14 == 0&&count!=0)
System.out.println(" ");
System.out.print("t"+k + " t");
count+=2;
}
firstDay=(firstDay+30)%7;
}
}
}
public static boolean isLeap(int year){
//判断是否是闰年
return (year%4==0&&year%100!=0)||(year%400==0);
}
}