Java while loop and branching

while loop

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++;
        }
    }
}

insert image description here
insert image description here

while loop example

insert image description here

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

insert image description here

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);
    }
}

insert image description here

Execute first and judge later

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);
    }
}

insert image description here

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 branch

insert image description here

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");

    }
}

output

大于37
不够
不及格
haha

switch

insert image description here

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("咋回事儿?");
        }
    }
}

output

咋回事儿?

Switch Notes

insert image description here

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,否则会出现穿透

        
    }
}

insert image description here

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 loop

hot key:fori + tab

insert image description here

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;
        }

    }
}

output

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

Sum of the first five integers

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);
    }
}

Guess you like

Origin blog.csdn.net/AdamCY888/article/details/131364889