switch(表达式){case1; 代码一;break;case2;代码2;break;-------default://以上情况均不是执行此代码}
举例: Scanner scanner =new Scanner(System.in);
System.out.println("输入分:");
int score=scanner.nextInt();
switch(score){
case 5: System.out.println("优秀");break;
case 4: System.out.println("优");break;
case 3:
case 2:
case 1:
System.out.println("不及格");
default:System.out.println("胡扯");
用switch语句制造简单的计算器:
Scanner scanner=new Scanner(System.in);
System.out.println("输入值一:");
int a=scanner.nextInt();
System.out.println("输入符号:");
char n=scanner.next().charAt(0);
System.out.println("输入值二:");
int b=scanner.nextInt();
switch (n) {
case '+':System.out.println(a+"+"+b+"="+(a+b));break;
case '-':System.out.println(a+"-"+b+"="+(a-b));break;
case '*':System.out.println(a+"*"+b+"="+(a*b));break;
case '/':System.out.println(a+"/"+b+"="+(a/b));break;
default:System.out.println("你在难为计算器,她不开心了!");