1. 程式人生 > 其它 >輸入指定的年月的月份有多少天

輸入指定的年月的月份有多少天

技術標籤:javajavac

/**
 * 編寫程式實現輸入指定的年月的月份有多少天
 * 
 */
    package cn;
    import java.util.Scanner;
    public class SwitchTest2 {
    public static void main(String[] args) {
	System.out.println("請輸入年月:");
	Scanner sc = new Scanner(System.in);
	int y = sc.nextInt();
	int m = sc.nextInt();
	int d = switch (m) {
	case 1, 3, 5, 7, 8, 10, 12 -> 31;
	case 4, 6, 9, 11 -> 30;
	default -> y % 4 == 0 && y % 100 != 0 || y % 400 == 0 ? 29 : 28;

	};
	System.out.printf("%d年%d月裡共有(%d)天",y,m,d);
}

}
在這裡插入圖片描述