Java中switch判断语句典型使用实例

时间:2021-07-10 23:09:42

下面一个是典型的switch语法运用的例子。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
public class JudgeMonth {
  public static void main(String[] arg){
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入月份:");
    int month = scan.nextInt();
    switch(month){
      case 12:
      case 1:
      case 2:
        System.out.println("冬季");
        break;
      case 3:
      case 4:
      case 5:
        System.out.println("春季");
        break;
      case 6:
      case 7:
      case 8:
        System.out.println("夏季");
        break;
      case 9:
      case 10:
      case 11:
        System.out.println("秋季");  
        break;
      default:
        System.out.println("没有这个月份!请重新输入.");
    }
  }
}