if循环结构

if 循环语句

if 双选择结构

语法:

if(布尔表达式){
//如果布尔表达式的值为true
}else {
//如果布尔表达式的值为false
}
import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的分数");

        int score = scanner.nextInt();
        if (score>=399){
            System.out.println("成功上岸");
        }
        else {
            System.out.println("进场打工");
        }

        scanner.close();
    }
}

 

if 多选择结构

语法:

if(布尔表达式1){
//如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
//如果布尔表达式2的值为true执行代码
}else if(布尔表达式3){
//如果布尔表达式3的值为true执行代码
}else{
如果以上布尔表达式的都不为true执行代码
}

注意:if 语句至多有一个else语句,

else语句放在所有else if 语句后面。

import java.util.Scanner;

public class ifDdemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数值");

        int score = scanner.nextInt();
        if(score<150 && score>=120){
            System.out.println("相当优秀");
        }else if(score<120 && score>=90) {
            System.out.println("一般般");
        }else if(score<90 && score>=60){
            System.out.println("垃圾");
        }else if(score<60 && score>=0){
            System.out.println("回去把小学重新读一遍");
        }else {
            System.out.println("请输入合法数值");
        }
        scanner.close();
    }
}

 

if 嵌套结构

语法:

if(布尔表达式1){
  //如果布尔表达式1的值为true执行代码
  if(布尔表达式2){
    //布尔表达式2的值为true执行代码
  }
}

 

posted on 2022-04-05 10:08  三岁学JAVA  阅读(232)  评论(0)    收藏  举报