其他分享
首页 > 其他分享> > 从1-100个数字猜数字,while循环猜中则结束循环,否则继续

从1-100个数字猜数字,while循环猜中则结束循环,否则继续

作者:互联网

package main;

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

public class Test {
    public static void main(String[] args) {
        Random r = new Random();
        int range = r.nextInt(100)+1;

        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请输入猜测的数字:");
            int digital = sc.nextInt();

            if (digital > range){
                System.out.println("您猜测的数字太大了!");
            }else if(digital < range){
                System.out.println("您猜测的数字太小了!");
            }else {
                System.out.println("恭喜您猜中了!");
                break;
            }
        }
    }

}

标签:数字,System,Scanner,while,循环,digital,println,range,out
来源: https://blog.csdn.net/qq_60054076/article/details/122244715