问题:输出每月第一天是星期几
//显示每月的第一天是星期几public class homework{
public static void main(String[] args) {
//create a Scanner
Scanner input = new Scanner(System.in);
//prompt the user to enter the year and month
System.out.print("Please input the year:");
int year = input.nextInt();
//What day is the first day?
System.out.print("Please input the first day is:");
int firstDay = input.nextInt();
switch(firstDay) {
case 0:System.out.println("The first day is " + "Sunday");break;
case 1:System.out.println("The first day is " + "Monday");break;
case 2:System.out.println("The first day is " + "Tuesday");break;
case 3:System.out.println("The first day is " + "Wednesday");break;
case 4:System.out.println("The first day is " + "Thursday");break;
case 5:System.out.println("The first day is " + "Friday");break;
case 6:System.out.println("The first day is " + "Saturday");break;
}
int day1 = 0,day2 = 0;
int[] run= {31,29,31,30,31,30,31,31,30,31,30,31};;
int[] ping = {31,28,31,30,31,30,31,31,30,31,30,31};
int Day1 = 0,Day2 = 0;
int i = 0,k = 0,temp = 1;
while(temp <= 12) {
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
Day1 += run[i] + 1;
day1 = Day1 % 7;
switch(day1) {
case 0:System.out.println("The month of first day is " + "Sunday");break;
case 1:System.out.println("The month of first day is " + "Monday");break;
case 2:System.out.println("The month of first day is " + "Tuesday");break;
case 3:System.out.println("The month of first day is " + "Wednesday");break;
case 4:System.out.println("The month of first day is " + "Thursday");break;
case 5:System.out.println("The month of first day is " + "Friday");break;
case 6:System.out.println("The month of first day is " + "Saturday");break;
}
i++;
}
else {
Day2 += ping[k] + 1;
day2 = Day2 % 7;
switch(day1) {
case 0:System.out.println("The month of first day is " + "Sunday");break;
case 1:System.out.println("The month of first day is " + "Monday");break;
case 2:System.out.println("The month of first day is " + "Tuesday");break;
case 3:System.out.println("The month of first day is " + "Wednesday");break;
case 4:System.out.println("The month of first day is " + "Thursday");break;
case 5:System.out.println("The month of first day is " + "Friday");break;
case 6:System.out.println("The month of first day is " + "Saturday");break;
}
k++;
}
temp++;
}
input.close();
}
}
我输出来的结果不正确,是一些重复的星期
求解答