Java while循环和分支

本文展示了Java编程中几种基本的循环结构——while、do-while和for循环的用法,以及if条件分支和switch-case语句的示例。在while循环中,通过不同的条件控制循环次数;do-while循环至少会执行一次;for循环适用于已知循环次数的情况。此外,还强调了switch-case语句的注意事项,如支持的数据类型和避免case穿透。
摘要由CSDN通过智能技术生成

while 循环

package com.ithema.spedkey;

public class demo6241 {
    public static void main(String[] args) {
        // 理解while
        int i = 0;
        while (i < 5){
            System.out.println("Hello word");
            i++;
        }
    }
}

在这里插入图片描述
在这里插入图片描述

while 循环案例

在这里插入图片描述

package com.ithema.loop;

public class test {
    public static void main(String[] args) {
        // 使用while
        double peak = 8848860;
        double paper = 0.1;

        int i = 0;
        while (paper < peak){

            paper *= 2;
            i++;
        }
        System.out.println("纸张折叠了"+i+"次");
        System.out.println(paper);
    }
}

do-while

在这里插入图片描述

package com.ithema.loop;

public class test {
    public static void main(String[] args) {
        // 使用do-while
       int i = 0;
       do{
           System.out.println("Hello world");
           i++;
       } while (i < 3);
    }
}

在这里插入图片描述

先执行 后判断

package com.ithema.loop;

public class test {
    public static void main(String[] args) {
        // 使用do-while

       int i = 0;
       do{
           System.out.println("Hello world");
           i++;
       } while (i < 3);

        // 先执行 后判断
       do{
           System.out.println("heheheheheheh");
       } while(false);
    }
}

在这里插入图片描述

package com.ithema.loop;

public class test {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println("hello");
        }
        // 不能识别i
    }
}

if分支

在这里插入图片描述

package com.ithema.loop;

public class test {
    public static void main(String[] args) {
        double t = 38.9;
        if (t > 37){
            System.out.println("大于37");
        }

        double money = 90;
        if (money > 99){
            System.out.println("够了");
        }
        else{
            System.out.println("不够");
        }

        int score = 40;
        if (score >= 90){
            System.out.println("优秀");
        }
        else if (score >= 80){
            System.out.println("良好");
        }
        else if (score >= 60){
            System.out.println("及格");
        }
        else{
            System.out.println("不及格");
        }

        // 一种情况
        int a = 17;
        if (a < 30) System.out.println("haha");

    }
}

输出

大于37
不够
不及格
haha

switch

在这里插入图片描述

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        String week = "周三";
        switch (week) {
            case "周一":
                System.out.println("周一啊");
                break;
            case "周二":
                System.out.println("周二啊");
                break;
            default:
                System.out.println("咋回事儿?");
        }
    }
}

输出

咋回事儿?

switch 注意事项

在这里插入图片描述

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        // 支持:byte、short、int、char、string等
        // 不支持double、float、long
        int a = 10;
        switch (a) {
            case 10:
                System.out.println(10);

        }
        // case 给出的值不允许重复,只能是字面量,不能是变量
        // 不要忘记写break,否则会出现穿透

        
    }
}

在这里插入图片描述

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        // 周一打游戏
        // 周二打游戏
        // 周三打游戏
        // 周四做饭
        // 周五做饭
        // 周六什么
        // 周七啥

        String week = "周三";
        switch (week){
            case "周一":
            case "周二":
            case "周三":
                System.out.println("打游戏");
                break;
            case "周四":
            case "周五":
                System.out.println("做饭");
                break;
            case "周六":
                System.out.println("什么");
                break;
            case "周天":
                System.out.println("啥");
                break;
            default:
                System.out.println("none");
        }
    }
}

for 循环

快捷键:fori + tab

在这里插入图片描述

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println("hello world"+i);
        }

        System.out.println("-------------------------");

        for (int i = 2; i < 5; i++) {
            System.out.println("hello world"+i);
        }

        System.out.println("-------------------------");

        for (int i = 1; i < 15; i+=2) {
            System.out.println("hello world"+i);
        }

        System.out.println("-------------------------");

        for (int i = 1; i < 10; i+=2) {
            System.out.println("hello world"+i);
            i-=1;
        }

    }
}

输出

hello world0
hello world1
hello world2
hello world3
hello world4
-------------------------
hello world2
hello world3
hello world4
-------------------------
hello world1
hello world3
hello world5
hello world7
hello world9
hello world11
hello world13
-------------------------
hello world1
hello world2
hello world3
hello world4
hello world5
hello world6
hello world7
hello world8
hello world9

前五整数求和

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <= 5 ; i++) {
            count += i;
        }
        System.out.println(count);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高山莫衣

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值