Java – 需要使我的循环不仅在三次猜测后结束,而且还要显示正确的数字。

huangapple go评论72阅读模式
英文:

Java - Need my loop to not only end after three guesses but display the correct number as well

问题

import java.util.Scanner;

public class GuessNumberDoWhileA {
    public static void main(String[] args) {
        
        // 生成1-10之间的随机数
        int number = (int) (Math.random() * 9 + 1);
        int count = 0;
        Scanner Input = new Scanner(System.in);
        
        // 提示用户猜一个1到10之间的数
        System.out.println("猜一个1到10之间的数");
        
        while (count < 3) {    
            count++;
            
            System.out.print("\n请输入你的猜测:");
            int guess = Input.nextInt();
            
            if (guess == number)
                System.out.println("正确,数字是:" + number);
            else if (guess > number)
                System.out.println("你猜的数字太大了,请重试!");
            else if (guess < number)
                System.out.println("你猜的数字太小了,请重试!");
        }
        System.out.println("三次猜测结束,数字是:" + number);
    }
}
英文:

I'm trying to have my loop only occur three times. So if the user doesn't guess the correct number after their third guess then the loop ends which I have but it doesn't display what the number was. I need the number displayed after the third guess but not sure why it's not displaying the correct number.

import java.util.Scanner;


public class GuessNumberDoWhileA {
public static void main(String[] args) {
	
	//Generate random number from 1-10
	int number = (int) (Math.random()*9 + 1);
	int count = 0;
	//Auto Generated Method stub
	Scanner Input = new Scanner(System.in);
	
	//Tell the user to guess a number
	System.out.println(&quot;Guess a number between 1 and 10&quot;);
	
	//int guess = -1;
	
	//while (guess != number) {
	  while (count &lt; 3) {	
		count++;
		
		
		System.out.print(&quot;\nEnter your guess: &quot;);
		int guess = Input.nextInt();
		
		if (guess == number)
			System.out.println(&quot;Correct the number was &quot; + number);
		else if (guess &gt; number)
			System.out.println(&quot;Your guess is to high try again!&quot;);
		else if (guess &lt; number)
			System.out.println(&quot;Your guess is to low try again!&quot;);
		else 
			System.out.println(&quot;The correct number is &quot; + number);
	}
			System.out.println(&quot;The number was &quot; + number);

}

}

答案1

得分: 1

你需要一个名为 guessed 的布尔变量,用于检查用户是否成功猜对了数字。这个布尔变量的初始值应为 false

在循环中,你不需要最后的 else 语句。如果用户猜对了数字,将 guessed 变量设置为 true 并跳出循环。循环结束后,检查 guessed 变量是否为 false。如果是 false,意味着用户未能猜出数字,因此向用户显示正确的数字。

如果用户成功猜出数字,那么循环中的第一个 if 语句将会在控制台上打印出正确的数字并跳出循环。它还会将布尔变量设置为 true,这样正确的数字就只会在控制台上打印一次。

boolean guessed = false;

while (count < 3) {
    count++;

    System.out.print("请输入你的猜测:");
    int guess = Input.nextInt();

    if (guess == number) {
        System.out.println("正确,数字是:" + number);
        guessed = true;
        break;
    } else if (guess > number)
        System.out.println("你猜的数字太大了,请重试!");
    else if (guess < number)
        System.out.println("你猜的数字太小了,请重试!");
}

if (!guessed) System.out.println("数字是:" + number);
英文:

You need a boolean variable that can be used to check whether user was able to guess the number correctly or not. Initial value of this boolean variable should be false.

You don't need the last else statement in the loop. If user guesses the number correctly, set the boolean variable to true and break out of the loop. After the loop, check if the boolean variable is false or not. If it is false, that means user was not able to guess the number, so display the correct number to the user.

If user is able to guess the number then the first if statement in the loop will print the correct number on the console and break out of the loop. It will also set the boolean variable to true, so correct number will be printed only once on the console.

boolean guessed = false;
    
while (count &lt; 3) {   
    count++;
        
    System.out.print(&quot;\nEnter your guess: &quot;);
    int guess = Input.nextInt();
        
    if (guess == number) {
        System.out.println(&quot;Correct the number was &quot; + number);
        guessed = true;
        break;
    }
    else if (guess &gt; number)
        System.out.println(&quot;Your guess is to high try again!&quot;);
    else if (guess &lt; number)
        System.out.println(&quot;Your guess is to low try again!&quot;);
}
    
if (!guessed) System.out.println(&quot;Number was: &quot; + number);

huangapple
  • 本文由 发表于 2020年9月4日 21:42:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63742365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定