【Java开发指南 | 第十三篇】Java条件语句

读者可订阅专栏:Java开发指南 |【CSDN@秋说】

在这里插入图片描述

Java 中的条件语句用于根据条件来执行不同的代码块。条件语句允许程序根据表达式的结果选择性地执行代码。

条件语句分为if 语句、if…else 语句、if…else if…else 语句、嵌套的 if…else 语句及switch语句五种。

if 语句

if 语句用于在条件为真时执行一段代码块。如果条件为真,则执行 if 语句中的代码块;如果条件为假,则跳过 if 语句中的代码块。

if (condition) {
    // 在条件为真时执行的代码块
}
public class ice {
    public static void main(String[] args) {
        int i = 0;
        if(i==1)
        {
            System.out.println("ice");
        }
    }
}

在这里插入图片描述

if…else 语句

if...else 语句用于在条件为真时执行一个代码块,而在条件为假时执行另一个代码块。

if (condition) {
    // 在条件为真时执行的代码块
} else {
    // 在条件为假时执行的代码块
}
public class ice {
    public static void main(String[] args) {
        int i = 0;
        if(i==1)
        {
            System.out.println("ice");
        }
        else {
            System.out.println("ICE");
        }
    }
}

在这里插入图片描述

if…else if…else 语句

if...else if...else 语句用于在多个条件之间进行选择,它允许您测试多个条件并执行相应的代码块。当条件列表中的条件顺序地被检查,并且第一个为真时,与该条件相关联的代码块将被执行。如果没有条件为真,则执行最后的 else 块(如果存在)。

if (condition1) {
    // 在条件1为真时执行的代码块
} else if (condition2) {
    // 在条件2为真时执行的代码块
} else {
    // 在以上条件都不为真时执行的代码块
}
public class ice {
    public static void main(String[] args) {
        int i = 0;
        if(i==1)
        {
            System.out.println("ice");
        }
        else if(i==2){
            System.out.println("ICE");
        }
        else{
            System.out.println("choice");
            //i!=1,i!=2,所以输出choice
        }
    }
}

在这里插入图片描述

嵌套的 if…else 语句

我们可以在另一个 if 或者 else if 语句中使用 if 或者 else if 语句。

语法:

if(布尔表达式 1){
   如果布尔表达式 1的值为true执行代码
   if(布尔表达式 2){
      如果布尔表达式 2的值为true执行代码
   }
}
public class ice {
    public static void main(String[] args) {
       int x=1;
       if(x==1) //成立
       {
           x++;
           if(x==2) //成立
           {
               System.out.println(x);
           }
       }
    }
}

在这里插入图片描述

switch 语句

switch 语句用于根据表达式的值选择性地执行代码块。它是一种更灵活的条件语句,适用于需要测试多个可能值的情况。

switch (expression) {
    case value1:
        // 当表达式的值等于 value1 时执行的代码块
        break;
    case value2:
        // 当表达式的值等于 value2 时执行的代码块
        break;
    // 可以有更多的 case 语句
    default:
        // 当表达式的值不匹配任何 case 时执行的代码块
}

switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型。

int变量类型实例:

import java.util.Scanner;
public class ice {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt(); // 从键盘获取整数值赋给变量 x
        switch (x){
            case 1:
                System.out.println(1+'!');
                break;
            case 2:
                System.out.println(2+'!');
                break;
            case 3:
                System.out.println(3+'!');
                break;
            default:
                System.out.println("Null");
        }
        scanner.close();
    }
}

在这里插入图片描述

char变量类型实例:

public class ice {
    public static void main(String args[]){
        char grade = 'A';
        switch(grade)
        {
            case 'A' :
                System.out.println("好");
                break;
            case 'B' :
            case 'C' :
                System.out.println("好好");
                break;
                //若grade为B或C,均输出“好好”
            case 'D' :
                System.out.println("好好好");
                break;
            case 'F' :
                System.out.println("好好好好");
                break;
            default :
                System.out.println("好好好好好");
        }
    }
}

在这里插入图片描述

同时我们也可以在switch中灵活运用break:

import java.util.Scanner;
public class ice {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int x = scanner.nextInt(); // 从键盘获取整数值赋给变量 x
        switch (x){
            case 1:
                System.out.println(1+"!");
                break;
            case 2:
                System.out.println(2+"!");
            case 3:
                System.out.println(3+"!");
                break;
            default:
                System.out.println("Null");
        }
        scanner.close();
    }
}

在这里插入图片描述

当我们输入2时,进入case 2模块,但该模块没有break,导致输出2!后仍进入case 3模块,进而输出3!,接着跳出循环。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋说

感谢打赏,祝你平安喜乐。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值