英文:
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("Guess a number between 1 and 10");
//int guess = -1;
//while (guess != number) {
while (count < 3) {
count++;
System.out.print("\nEnter your guess: ");
int guess = Input.nextInt();
if (guess == number)
System.out.println("Correct the number was " + number);
else if (guess > number)
System.out.println("Your guess is to high try again!");
else if (guess < number)
System.out.println("Your guess is to low try again!");
else
System.out.println("The correct number is " + number);
}
System.out.println("The number was " + 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 < 3) {
count++;
System.out.print("\nEnter your guess: ");
int guess = Input.nextInt();
if (guess == number) {
System.out.println("Correct the number was " + number);
guessed = true;
break;
}
else if (guess > number)
System.out.println("Your guess is to high try again!");
else if (guess < number)
System.out.println("Your guess is to low try again!");
}
if (!guessed) System.out.println("Number was: " + number);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论