java switch case语句例子
时间: 2023-04-26 07:02:28 浏览: 117
```java
int month = 8;
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month");
break;
}
```
上面的程序中,switch语句会检查变量month是否等于1,2,3等等。如果等于1,就会执行第一个case语句。如果等于2,就会执行第二个case语句。如果都不符合,就会执行default语句。
阅读全文