满足那个条件,就从那个入口进入,没有break就继续(是这样设计的,需要自己加break;),一般都是要加break的。 switch (表达式) 表达式只能是【整型、char、String.】
import java.util.Scanner;public class testSwitch {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入一个数字(1-4):");int number = scanner.nextInt();System.out.print("数字是:"+number+'\n');String season;switch (number) {case 1:season = "春季";break;case 2:season = "夏季";break;case 3:season = "秋季";break;case 4:season = "冬季";break;default:season = "输入错误";break;}System.out.println("对应的季节是:" + season);scanner.close();}
}