switch multiple selection structure
-
Another implementation of the multiple-choice structure is the switch case statement.
-
The switch case statement determines whether a variable is equal to a certain value in a series of values, and each value is called a branch.
-
The variable type in the switch statement can be:
- byte, short, int or char
- Starting from Java SE 7 switch supports string String type
- At the same time, the case label must be a string constant or literal.
-
grammar:
switch(expression){
case value: //语句
break;//可选
case value: //语句
break;//可选
//可以有任意数量的case语句
default://可选
//语句
}
import java.util.Scanner;
public class A0116 {
//case穿透现象: 如果没有break语句;在case匹配成功后继续输出
//switch是匹配一个具体的值
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
System.out.println("你最喜欢什么季节");
String weather = a.nextLine();
switch(weather){
case "春天":
System.out.println("春天在哪里呀");
break;//可选 (不写会穿透)
case "夏天":
System.out.println("蚊子咬死你呀");
break;
case "秋天":
System.out.println("冬天等着你呀");
break;
case "冬天":
System.out.println("你真不怕冷呀");
break;
default://可选
System.out.println("季节,你懂?");
a.close();
run:
你最喜欢什么天气
冬天
你真不怕冷呀
Decompile, view the class file
Decompile java-class (bytecode file) ----- Decompile (via IDEA)
To view the class file, click on the project structure and copy the path:
After finding the corresponding class file, copy the class file to the folder where java is stored, and then you can view the code through IDEA