今天照著書上教的switch語法,寫了一個判斷是否為閏年的程式,最後要印出「年分+月份+天數」。

switch語法:

switch(變數名稱或運算式){

   case 1:

   case'A':

    break;

    default:

}

比對【字元】時,要記得用單引號

import java.util.Scanner;

public class MainClass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year, month, numDays = 0; //這邊一開始沒有初始化,導致numDays不能執行
        System.out.print("請輸入年分:");
        year = sc.nextInt();
        System.out.print("請輸入月分:");
        month = sc.nextInt();

        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDays = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30;
                break;
            case 2:
                if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) {
                    numDays = 29;
                } else {
                    numDays = 28;
                }
        }
        System.out.print("年分:" + year + "\n\n月份:" + month + "\n\n天數:" + numDays);
    }

 

在修改成可以判斷是否輸入無效的月份 ():

import java.util.Scanner;

public class MainClass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year, month, numDays = 0;
        System.out.print("請輸入年分:");
        year = sc.nextInt();
        System.out.print("請輸入月分:");
        month = sc.nextInt();

        if(month <= 12 ){
            switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDays = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30;
                break;
            case 2:
                if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) {
                    numDays = 29;
                } else {
                    numDays = 28;
                }
        }
            System.out.print("年分:" + year + "\t月份:" + month + "\t天數:" + numDays);
        }else{
            System.out.print("無效月份");
        }
    }
}

 

使用while:

使用while更好,因為可以重新讓使用者輸入正確的月份

import java.util.Scanner;

public class MainClass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year, month, numDays = 0; //這邊一開始沒有初始化,導致numDays不能執行
        System.out.print("請輸入年分:");
        year = sc.nextInt();
        System.out.print("請輸入月分:");
        month = sc.nextInt();

        while(month>12){
            System.out.println("無效月份");
            month = 0;
            System.out.print("請輸入月分:");
            month = sc.nextInt();
        }

        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDays = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30;
                break;
            case 2:
                if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) {
                    numDays = 29;
                } else {
                    numDays = 28;
                }
        }
        System.out.print("年分:" + year + "\n\n月份:" + month + "\n\n天數:" + numDays);
    }
}

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Lillian 的頭像
    Lillian

    安安的code日記

    Lillian 發表在 痞客邦 留言(0) 人氣()