switch
程序员文章站
2022-05-02 08:16:16
...
流程控制
- 顺序结构
public static void main(String[] args) {
System.out.println("hello01");
System.out.println("hello02");
System.out.println("hello03");
}
}
- 选择结构
public static void main(String[] args) {
//考试60分几个,否者不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
double score = scanner.nextDouble();
if(score>=60){
System.out.println("及格");
}else{
System.out.println("不及格");
}
scanner.close();
}
}
- 多选择结构
public static void main(String[] args) {
//考试60分几个,否者不及格
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
double score = scanner.nextDouble();
if(score==100){
System.out.println("满分");
}else if(score<100 && score>=90){
System.out.println("A级");
}else if(score<90 && score>=80){
System.out.println("B级");
}else if(score<80 && score>=60){
System.out.println("C级");
}else {
System.out.println("不及格");
}
scanner.close();
}
- switch多选择结构
public static void main(String[] args) {
//case 穿透 switch 匹配一个具体的值
char grade = 'C';
switch (grade){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("一般");
break;
case 'C':
System.out.println("差");
break;
default:
System.out.println("未知的东西");
}
}
- 关于switch的字符串输入
public static void main(String[] args) {
//字符的本质还是数字
//反编译java---class (字节码文件)---反编译(idea)
String name = "PG";
switch (name){
case "PG":
System.out.println("bingo");
break;
case "one":
System.out.println("congratulation");
break;
default:
System.out.println("poor boy");
}
}
上一篇: switch和if语句输出成绩等级