这个问题呢,你也是可以用if-else语来解决的,但是可能代码比较冗余。而java里面的switch-case可以很好的解决这个问题,你不要一想到switch就想到了break,其实有些时候不加break也是可以的,注意灵活运用。
话不多说,直接上代码;
import java.util.Scanner;
class CountDay{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入月份:");
int month = scan.nextInt();
System.out.println("请输入日子:");
int day = scan.nextInt();
//用来表示第几天
int sumDays = 0;
switch(month){
case 12:
sumDays += 30;
case 11:
sumDays += 31;
case 10:
sumDays += 30;
case 9:
sumDays += 31;
case 8:
sumDays += 31;
case 7:
sumDays += 30;
case 6:
sumDays += 31;
case 5:
sumDays += 30;
case 4:
sumDays += 31;
case 3:
sumDays += 28;
case 2:
sumDays += 31;
case 1:
sumDays += day;
}
System.out.println("结果为:" + sumDays);
}
}