【Java】——程序逻辑控制(构建稳健代码的基石)

在这里插入图片描述

🎁个人主页:User_芊芊君子
🎉欢迎大家点赞👍评论📝收藏⭐文章
🔍系列专栏:【Java】内容概括
在这里插入图片描述

在这里插入图片描述

在Java编程的世界里,逻辑控制就像是程序的“大脑”,指挥着代码按照预定的规则和路径执行。对于Java小白来说,掌握逻辑控制结构是迈向编程高手的关键一步。本文将深入探讨Java中的逻辑控制,帮助大家打下坚实的基础。

一.顺序结构

顺序结构:按照代码书写的顺序一行一行执行。

public class Test {
    public static void main(String[] args) {
        System.out.println("a");
        System.out.println("b");
        System.out.println("c");
    }
}

运行结果:
在这里插入图片描述

二.分支结构

1.if 语句

1.1 语法格式1

if(布尔表达式){
//语句
}

如果布尔表达式结果为true,执⾏if中的语句,否则不执⾏

eg:如果我的分数大于90,就会得到奖状

public class Test {
    public static void main(String[] args) {
        int score = 99;
        if(score>90){
            System.out.println("恭喜你,得到一个奖状");
        }
    }
}

在这里插入图片描述

1.2 语法格式2

if(布尔表达式){
    //语句1
}else{
    //语句2
}

如果布尔表达式结果为true,则执⾏if中语句,否则执⾏else中语句。

public class Test {
    public static void main(String[] args) {
        int score = 88;
        if(score>90){
            System.out.println("恭喜你,得到一个奖状");
        }else{
            System.out.println("继续努力");
        }
    }
}

在这里插入图片描述

1.3 语法格式3

if(布尔表达式1{
    //语句1
}else if(布尔表达式2{
    //语句2
}else{
    //语句3
}

表达式1成⽴,执⾏语句1,否则表达式2成⽴,执⾏语句2,否则执⾏语句3
eg:

• 分数在 [90, 100] 之间的,为优秀
• 分数在 [80, 90) 之前的,为良好
• 分数在 [70, 80) 之间的,为中等
• 分数在 [60, 70) 之间的,为及格
• 分数在 [ 0, 60) 之间的,为不及格
• 错误数据

public class Test {
    public static void main(String[] args) {
        int score = 100;
        if (score >= 90) {
            System.out.println("优秀");
        } else if (score >= 80 && score < 90) {
            System.out.println("良好");
        } else if (score >= 70 && score < 80) {
            System.out.println("中等");
        } else if (score >= 60 && score < 70) {
            System.out.println("及格");
        } else if (score >= 0 && score < 60) {
            System.out.println("不及格");
        } else {
            System.out.println("错误数据");
        }
    }
}

在这里插入图片描述
【练习】
1.判断奇偶

public class Test {
    public static void main(String[] args) {
        int num = 6;
        if (num % 2 == 0) {
            System.out.println("偶数");
        }else{
            System.out.println("奇数");
        }
    }
}

在这里插入图片描述
2.判断正负

public class Test {
    public static void main(String[] args) {
        int num = 8;
        if (num > 0){
            System.out.println("正数");
        }else if(num < 0){
            System.out.println("负数");
        }else{
            System.out.println("0");
        }
    }
}

在这里插入图片描述
3.判断闰年

public class Test {
    public static void main(String[] args) {
        int year = 2025;
        if((year % 4 == 0 && year % 100 != 0)||(year % 400 ==0)){
            System.out.println("闰年");
        }else{
            System.out.println("不是闰年");
        }
    }
}

在这里插入图片描述
博主在这里提醒一下,咱们的代码风格不能像C语言那样,虽然是合法的,但在Java中,推荐以上这种风格

2.switch 语句

2.1 基本语法

switch(表达式){
       case 常量值1{
       语句1[break;]
       }
       case 常量值2{
       语句2[break;]
       }
       ...
       default:{
       内容都不满足时执行语句;
       [break;]
       }
}

2.2执行流程

  • 先计算表达式的值
  • 和case依次⽐较,⼀旦有响应的匹配就执⾏该项下的语句,直到遇到break时结束
  • 当表达式的值没有与所列项匹配时,执⾏default

2.3 输出星期

public class Test {
    public static void main(String[] args) {
        int day = 1;
        switch(day){
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期日");
                break;
            default:
                System.out.println("输入有误");
                break;
            }
        }
    }

在这里插入图片描述

2.4 注意事项

  • 多个case后的常量值不可以重复
  • switch的括号内只能是以下类型的表达式:
    基本类型byte、char、short、int,注意不能是long类型 ◦
    引⽤类型String常量串、枚举类型
  • break 不要遗漏,否则会失去 “多分⽀选择” 的效果 -
  • switch 不能表达复杂的条件
  • switch 虽然⽀持嵌套, 但是很丑,⼀般不推荐~
    综上,我们发现, switch 的使⽤局限性是⽐较⼤的

三.循环结构

1. while 循环

1.1 基本语法格式

while(循环条件){
      循环语句;
      }

循环条件为 true, 则执⾏循环语句; 否则结束循环.

1.2【练习】

1.打印1~10

public class Test {
    public static void main(String[] args) {
        int num = 1;
        while (num <= 10){
            System.out.println(num);
            num++;
        }
    }
}

在这里插入图片描述
2.计算1~100的和

public class Test {
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        while(i <= 100){
            sum += i;
            i++;
        }
        System.out.println(sum);
    }
}

在这里插入图片描述
3.计算5的阶乘

public class Test {
    public static void main(String[] args) {
        int n = 1;
        int result = 1;
        while(n <= 5){
            result *= n;
            n++;
        }
        System.out.println(result);
    }
}

在这里插入图片描述
4.计算 1! + 2! + 3! + 4! + 5!

public class Test {
    public static void main(String[] args) {
        int x = 1;
        int ret = 0;
        while (x <= 5) {
            int sum = 1;
            int n = 1;
            while (n <= x) {
                sum *= n;
                n++;
            }
            ret += sum;
            x++;
        }
        System.out.println(ret);
    }
}

在这里插入图片描述

【注意】

  1. 和 if 类似, while 下⾯的语句可以不写 { } , 但是不写的时候只能⽀持⼀条语句. 建议还是加上 { }
  2. 和 if 类似, while 后⾯的 { 建议和 while 写在同⼀⾏.
  3. 和 if 类似, while 后⾯不要多写 分号, 否则可能导致循环不能正确执⾏.

2.break

break 的功能是让循环提前结束.

eg:找到 100 - 200 中第⼀个 3 的倍数

public class Test {
    public static void main(String[] args) {
        int num = 100;
        while(num <= 200){
            if(num % 3 == 0){
                System.out.println(num);
                break;
            }
            num++;
        }
    }
}

在这里插入图片描述

4.continue

ontinue 的功能是跳过这次循环, ⽴即进⼊下次循环.
eg:

public class Test {
    public static void main(String[] args) {
        int num = 100;
        while(num <= 200){
            if(num % 3 != 0){
                num++;
                continue;
            }
            System.out.println(num);
            num++;
        }
    }

在这里插入图片描述

5.for循环

5.1 基本语法

for(表达式1;布尔表达式2;表达式3{
         表达式4}

• 表达式1: ⽤于初始化循环变量初始值设置,在循环 最开始时执⾏,且只执⾏⼀次
• 表达式2: 循环条件,满则循环继续,否则循环结束
• 表达式3: 循环变量更新⽅式

5.2 练习

1.打印1-10

public class Test {
    public static void main(String[] args) {
        for (int i = 0; i <= 10; i++) {
            System.out.println(i);
        }
    }
}

在这里插入图片描述
2.打印1-100的和

public class Test {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        System.out.println(sum);
    }
}

在这里插入图片描述
3.计算 5 的阶乘

public class Test {
    public static void main(String[] args) {
        int ret = 1;
        for (int i = 1; i <= 5; i++) {
            ret *= i;
        }
        System.out.println(ret);
    }
}

在这里插入图片描述
4.计算 1! + 2! + 3! + 4! + 5!

public class Test {
    public static void main(String[] args) {
        int sum =0;
        for (int i = 1; i <= 5 ; i++) {
            int tmp = 1;
            for(int j = 1;j <= i; j++){
                tmp *= j;
            }
            sum +=tmp;
        }
        System.out.println(sum);
    }
}

在这里插入图片描述

【注意】

  1. 和 if 类似, for 下⾯的语句可以不写 { } , 但是不写的时候只能⽀持⼀条语句. 建议还是加上 { }
  2. 和 if 类似, for 后⾯的 { 建议和 while 写在同⼀⾏.
  3. 和 if 类似, for 后⾯不要多写 分号, 否则可能导致循环不能正确执⾏.
  4. 和while循环⼀样,结束单趟循环⽤continue,结束整个循环⽤break
  5. 表达式2如果缺失,代表该循环为死循环。

6. do while 循环

【基本语法】

do{
循环语句;
}while(循环条件);

先执⾏循环语句, 再判定循环条件,循环条件成⽴则继续执⾏,否则循环结束。

public class Test {
    public static void main(String[] args) {
        int num = 1;
        do{
            System.out.println(num);
            num++;
        }while(num <= 10);
    }
}

在这里插入图片描述

【注意事项】

  1. do while 循环最后的分号不要忘记
  2. ⼀般 do while 很少⽤到, 更推荐使⽤ for 和 while.

【总结】

在Java编程的世界里,逻辑控制就像是程序的“大脑”,指挥着代码按照预定的规则和路径执行。对于Java小白来说,掌握逻辑控制结构是迈向编程高手的关键一步。本文将深入探讨Java中的逻辑控制,帮助大家打下坚实的基础。

在这里插入图片描述

评论 151
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值