标题使用switch实现各月份天数的判断
(2月天数需要判断闰年)
import java.util.Scanner;
public class Demo05
{
public static void main(String[] args)
{
System.out.println("please input month");
Scanner console = new Scanner(System.in);
int month = console.nextInt();
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30");
break;
case 2:
System.out.println("please input year:");
int year = console.nextInt();
boolean isleapyear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
int day = isleapyear ? 29 : 28;
System.out.println(day);
break;
default:
System.out.println("error");
break;
}
}
}