首页 > 其他分享 >数字猜大小,并统计直到猜对时输入的次数

数字猜大小,并统计直到猜对时输入的次数

时间:2023-03-15 10:55:29浏览次数:31  
标签:count guess 数字 int System 次数 println 输入 out

package com.example.practice;

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

public class Test10 {
    public static void main(String[] args) {
        Random random = new Random();
        int i = random.nextInt(100);
        System.out.println(i);
        Scanner scanner = new Scanner(System.in);
        int count = 0;
        while (true) {
            count++;
            System.out.println("请输入你要猜的数字:");
            int guess = scanner.nextInt();
            if (guess > i) {
                System.out.println("猜大了");
            } else if (guess < i) {
                System.out.println("猜小了");
            } else {
                System.out.println("猜对了,总共猜了"+count+"次");
                break;
            }
        }
    }

}

标签:count,guess,数字,int,System,次数,println,输入,out
From: https://www.cnblogs.com/faith007/p/17217707.html

相关文章