你的程序有两个错误。先说你想到的这个错误,因为在最后一个case中你将num赋值为day,所以不管前边num加了多少次,最后都会是day这个变量的值。再者,还有个逻辑错误,不是语法错误。case月份的时候应该是num+=上个月的天数,而不是当月的天数。最后在switch外面再加上day。
程序如下:import java.util.Scanner;
class Exer2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入年份:");
int year = scan.nextInt();
System.out.println("请输入月份:");
int month = scan.nextInt();
System.out.println("请输入日:");
int day = scan.nextInt();
int num = 0 ;
switch(month){
case 12:
num += 30;
case 11:
num += 31;
case 10:
num += 30;
case 9:
num += 31;
case 8:
num += 31;
case 7:
num += 30;
case 6:
num += 31;
case 5:
num += 30;
case 4:
num += 31;
case 3:
if((year % 4 ==0&&year % 100 != 0)||year % 400 == 0 ){
num += 29;
}else{
num += 28;
}
case 2:
num += 31;
}
num+=day;
System.out.println("这一天是当年的第"+ num +"天");
}
}
|