cuistudy

导航

if选择结构

if选择结构

if单选择结构

判断一个东西是否可行,然后再去执行

语法

if(){

//如果布尔表达式为true将执行的语句

}

![83654137677f70d4b659af82c624be4](C:\Users\tyqwww\Documents\WeChat Files\wxid_ps5t4nfqr5t122\FileStorage\Temp\83654137677f70d4b659af82c624be4.png)

``

package com.cuistudy.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语句不行

语法

if(){

//如果布尔表达式的值为true

}else{

//如果布尔表达式的值为false

}

![3fb9867025a56f5fd46f6fdb8ebe4e1](C:\Users\tyqwww\Documents\WeChat Files\wxid_ps5t4nfqr5t122\FileStorage\Temp\3fb9867025a56f5fd46f6fdb8ebe4e1.png)

``

package com.cuistudy.struct;

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(score+"及格");
        }else{
            System.out.println(score+"不及格");
        }
        scanner.close();
    }
}

``

package com.cuistudy.struct;

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容:");
        int a=scanner.nextInt();
        if (a>=1){
            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 if(布尔表达式4){

//如果布尔表达式4的值true执行代码

}

![3b7660e2d72cfd0c114bdb44e7d8f9a](C:\Users\tyqwww\Documents\WeChat Files\wxid_ps5t4nfqr5t122\FileStorage\Temp\3b7660e2d72cfd0c114bdb44e7d8f9a.png)

package com.cuistudy.struct;

import java.util.Scanner;

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

        /*
        if语句之多有1个else语句,else语句在所有的else if语句之后
        if语句可以有若干个else if语句,都必须在else语句之前
        一旦其中一个else if语句检测为true,其他的else if以及else语句都将跳过执行
         */
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        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("成绩不合法");
        }
        scanner.close();
    }

嵌套if结构

语法

if(布尔表达式1){

//如果布尔表达式1的值true执行代码

if(布尔表达式2){

//如果布尔表达式2的值true执行代码

}

}

posted on 2025-01-02 23:05  tyqwww  阅读(12)  评论(0)    收藏  举报