无限循环、循环嵌套

  • 一、无限循环
  • for 无限循环
  • while 无限循环
  • do-while 无限循环
  • 二、循环嵌套
  • 三、跳转关键字
  • break
  • continue
  • 四、生成随机数
  • random
  • 案例


一、无限循环

java 无限循环、循环嵌套_开发语言

for 无限循环

package com.ithema.loop;

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

        for (int i = 0 ; ;i++) {
            System.out.println("hello word");
        }
        
        System.out.println("-----------------------");
        
        for ( ; ;) {
            System.out.println("hello word");
        }
    }
}

while 无限循环

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
       while (true) {
           System.out.println("hello word");
       }
    }
}

do-while 无限循环

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
       do{
           System.out.println("hello word");
       } while(true);
    }
}

二、循环嵌套

java 无限循环、循环嵌套_jvm_02

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 5; j++) {
                String  out = String.format("第 %s 天 第 %s 句:",i+1,j+1);
                System.out.println(out+"我哈哈哈");
            }

        }
    }
}

打印三行四列星号

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

三、跳转关键字

java 无限循环、循环嵌套_jvm_03

break

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.println("hahah");
            if(i==3){
                break;
            }
        }
    }
}

输出

hahah
hahah
hahah

continue

package com.ithema.loop;

public class demo241 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {

            if(i==3){
                continue;
            }
            System.out.println("hahah"+i);
        }
    }
}

输出

hahah1
hahah2
hahah4
hahah5

四、生成随机数

random

java 无限循环、循环嵌套_System_04


快捷键:ctrl+alt +t

package com.ithema.loop;

import java.util.Random;

public class demo241 {
    public static void main(String[] args) {
        Random r = new Random();
        int i = 0;
        while (i < 5){
        int data = r.nextInt(10);
        System.out.println(data);
        i++;
        }

    }
}

生成1-10的随机数

package com.ithema.loop;

import java.util.Random;

public class demo241 {
    public static void main(String[] args) {
        Random r = new Random();
        int i = 0;
        while (i<10) {
            int data = r.nextInt(10);
            System.out.println(data+1);
            i++;
        }
    }
}

java 无限循环、循环嵌套_开发语言_05

案例

输入数字,提示比真实值大小,直到猜中数字!

package com.ithema.loop;

import java.util.Random;
import java.util.Scanner;

public class demo241 {
    public static void main(String[] args) {
        Random r = new Random();
        int data = r.nextInt(10) + 1;

        while(true){

            // 键盘输入数字

            Scanner scanner = new Scanner(System.in);

            System.out.print("请输入一个数字: ");
            int a = scanner.nextInt();

            if (a == data){
                System.out.println("猜中了");
                scanner.close();
                break;
            }
            else if (a > data){
                System.out.println("猜大了");
            }
            else{
                System.out.println("猜小了");
            }
        }
    }
}

输出

请输入一个数字: 4
猜小了
请输入一个数字: 10
猜大了
请输入一个数字: 8
猜大了
请输入一个数字: 6
猜中了