循环语句switch格式
switch (表达式){
case 目标值1:
执行语句1
break;
case 目标值2:
执行语句2
break;
......
case 目标值n:
执行语句n
break;
default:
执行语句n+1
break;
}
举例
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入1-7");
int str = sc.nextInt();
switch (str){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("请输入有效数字!");
}
}
}