选择结构

If单选择结构

  • 语法:if(布尔表达式){
    //值为真执行的语句
    }
    `

    package com.sjy.struct;

    import java.util.Scanner;

    public class IfDemo01 {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("请输入内容");
    String s = scanner.nextLine();
    //equals:判断字符串是否相等
    if (s.equals("hello")){
    System.out.println(s);
    }
    System.out.println("end");
    scanner.close();
    }

    }

`

If双选择结构

  • 语法:if(){
    //值为真执行语句
    }else{
    //值为假执行语句
    }
    `

    package com.sjy.struct;

    import javax.sql.rowset.serial.SQLOutputImpl;
    import java.sql.SQLOutput;
    import java.util.Scanner;

    public class IfDemo02 {
    public static void main(String[] args) {
    //考试分数大于等于60及格 小于60不及格
    Scanner scanner = new Scanner(System.in);
    System.out.println("输入成绩");
    int score = scanner.nextInt();

    if(score>=60){
    System.out.println("及格");
    }else{
    System.out.println("不及格");
    }

    }
    }

`

If多选择结构

  • 语法:

`

if(布尔表达式1){
//值为真执行语句
}else if(布尔表达式2){
//值为假执行语句
}else if(布尔表达式3){
//布尔表达式3值为真执行语句
}else{
//以上布尔值表达式都不为真执行代码
}

`

`

package com.sjy.struct;

import java.util.Scanner;

public class IfDemo03 {
public static void main(String[] args) {
//成绩大于60及格 <60不及格
Scanner scanner = new Scanner(System.in);
System.out.println("输入成绩");
float score = scanner.nextFloat();//可以接收小数

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>=70){
System.out.println("C");
}else if (score<70&&score>=60){
System.out.println("D");
}else if (score<60&&score>=0){
System.out.println("不及格");
}else {
System.out.println("不合法");
}
}
}

`

If嵌套结构

  • 语法

`

if(布尔表达式1){
//布尔表达式1值为真执行语句
if(布尔表达式2){
//布尔表达式2值为真执行语句
}
}

`

switch多选择结构

  • 多选择结构还有一个实现方法就是switch case语句

  • switch case语句判断一个变量与一系列值某个值是否相等 每个值成为一个分支

  • switch语句中的变量类型可以是:

    • byte,short,int或者char
    • 从javase7开始
    • switch支持字符串string类型
    • 同时case标签必须为字符串常量或字面量
      `

    package com.sjy.struct;

    public class SwitchDemo01 {
    public static void main(String[] args) {
    //
    char grade='c';
    switch (grade){
    case 'a':
    System.out.println("优秀");
    break;
    case 'b':
    System.out.println("良好");
    break;
    case 'c':
    System.out.println("及格");
    break;
    case 'd':
    System.out.println("不及格");
    break;
    default:
    System.out.println("不符合输入");
    }
    }
    }

`

`

package com.sjy.struct;

public class SwitchDemo02 {
public static void main(String[] args) {
String name="sjy";
//JDK7的新特性 表达式结果可以是字符串!!!
//字符的本质就是数字

//反编译 java----class(字节码文件)----反编译(idea)
switch (name){
case "myy":
System.out.println("马瑶瑶");
break;
case "sjy":
System.out.println("尚峻宇");
break;
default:
System.out.println("不是人");
}
}
}

`

posted @ 2021-05-21 01:07  mr42  阅读(74)  评论(0)    收藏  举报