选择语句:switch-case
格式:
switch(变量或者值){//byte short int cha String(jdk1.7)
case 值1:代码1;break;
case 值2:代码2;break;
.
.
.
.
default:代码3;break;//如果都不能匹配,执行default
}
注:
具有穿透性:执行某个case里的代码时如果没有break,继续指向下一个case,
直到遇到break为止
public static void main(String[] args) {
String str="A";
int i=0;
switch(str){
case "A":
i++;
System.out.println("选择了A选项");break;
case "B":
i++;
System.out.println("选择了B选项");break;
case "C":
i++;
System.out.println("选择了C选项");break;
default:
System.out.println("没有选择选项");break;
}
System.out.println("i="+i);//选择了A选项 i=1
}