SwitchDemo(1).java【輸入年份和月份,判斷某年某月有多少天】


//課堂習題:輸入年份和月份,判斷某年某月有多少天
import java.util.Scanner;
public class SwitchDemo{
    public static void main(String [] args){
        Scanner input=new Scanner(System.in);
        System.out.print("請輸入需要查詢的年份:");
        int year=input.nextInt();
        System.out.print("請輸入需要查詢的月份:");
        int month=input.nextInt();
        switch(month){
            case 1:case 3:case 5:case 7:case 8:case 10:case 12:
                System.out.println(year+"年"+month+"月有31天。");
                break;//每一個case后需要添加break跳出switch語句
            case 2:
                if(year%4==0&&year%100!=0||year%400==0){
                    System.out.println(year+"年"+month+"月有29天。");
                }else{
                    System.out.println(year+"年"+month+"月有28天。");
                }
                break;//此處break如果省略,將繼續向下執行,一直到遇到break為止或者是switch語句結束
            case 4:case 6:case 9:case 11:
                System.out.println(year+"年"+month+"月有30天。");
                break;
            default:
                System.out.println("ERROR!");
                //break;//default可以省略、此處的break也可以省略
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM