分支結構3
阿新 • • 發佈:2018-11-10
輸入一個數字,在控制檯輸出其對應的月份和該月份對應的天數。
package ch2; import java.util.*; public class SwitchOP { public static void main(String[] args) { System.out.println("請輸入月份:"); Scanner scanner=new Scanner(System.in); int month=scanner.nextInt(); switch(month){ case 1: System.out.println("一月有31天");break; case 2: System.out.println("二月有28/29天");break; case 3: System.out.println("三月有31天");break; case 4: System.out.println("四月有30天");break; case 5: System.out.println("五月有31天");break; case 6: System.out.println("六月有30天");break; case 7: System.out.println("七月有31天");break; case 8: System.out.println("八月有31天");break; case 9: System.out.println("九月有30天");break; case 10: System.out.println("十月有31天");break; case 11: System.out.println("十一月有30天");break; case 12: System.out.println("十二月有31天");break; default: System.out.println("無效月份."); } } }