如何显示尝试次数和仅对输入相同数字的1计数

问题描述 投票:0回答:1

帮助!它是猜测游戏,但我有一个问题,它需要连续打印用户输入的尝试次数,但只有一次计数到相同的次数。

public static void main(String [] args){//这里的TODO代码应用逻辑

int ans,guess;
final int max = 10;


Scanner jai = new Scanner(System.in);
Random no = new Random();

int att = 0;
ans = no.nextInt(max) + 1;

while (true){
    System.out.println("Enter a number between 1 to 10");
    guess = jai.nextInt();
    att += 1;
    att;

if (guess == ans){
    System.out.println("You are correct, the answer is :"+ ans);
    System.out.println(att +" attempts to find the correct number!");
     System.exit(0);
}else if (guess > ans){
    System.out.println("Too Large");
}else if (guess < ans){
    System.out.println("Too Small");
java random java.util.scanner
1个回答
0
投票

您需要记住上一次输入的数字,如果该数字与用户输入的新数字相同,则不要增加尝试次数计数器。

© www.soinside.com 2019 - 2024. All rights reserved.