英文:
Trying to loop 6 times with a while loop
问题
我正在尝试让我的程序循环6次。问题在于它继续运行,在循环了6次后并没有停止。我有answer、userGuess和numOfGuesses作为实例变量。
public static int guess(int userGuess) {
System.out.println("Enter your guess");
while (numOfGuesses <= 6) { // 这是我的问题
numOfGuesses = numOfGuesses + 1;
while (userGuess != answer) {
userGuess = scan.nextInt();
if (userGuess < answer) {
System.out.println("Too low, try again");
} else if (userGuess > answer) {
System.out.println("Too high, try again");
} else {
}
}
}
// 方法的其余部分正常工作
}
英文:
I'm trying to have my program loop 6 times. My problem is that it keeps going and doesn't stop after 6 times. I have answer, userGuess, and numOfGuesses as instance variables.
public static int guess(int userGuess){
System.out.println("Enter your guess");
while (numOfGuesses <= 6){ // this is my problem
numOfGuesses = numOfGuesses + 1 ;
while (userGuess != answer){
userGuess = scan.nextInt();
if(userGuess < answer){
System.out.println("Too low, try again");
}
else if(userGuess > answer){
System.out.println("Too high, try again");
}
else{
}
}
}
// rest of method works fine
答案1
得分: 1
这个循环结构可能不是你想要的。有两个 while 循环,实际上并没有限制猜测的次数 - 你所做的只是重新运行猜测程序 6 次。另外,由于内部的 while 循环执行直到答案完全正确,用户无法知道他们的猜测是太低还是太高。你可能想要像这样的结构:
public static int guess(int userGuess){
do {
System.out.println("Enter your guess: ");
userGuess = scan.nextInt();
if (userGuess < answer) {
System.out.println("Too low, try again");
} else if (userGuess > answer) {
System.out.println("Too high, try again");
} else {
// 待办:告诉用户他们猜对了
}
numOfGuesses = numOfGuesses + 1;
} while (numOfGuesses <= 6 && userGuess != answer);
// 在用户用完所有猜测次数的情况下,告诉他们他们没有猜对
// 待办:在这里返回一些内容,因为这个函数应该返回一个整数
}
还要注意底部的注释,直到方法返回内容之前,这个方法是无法编译的。我还假设所有引用的变量在其他地方已经定义。
我还对参数名为 userGuess
感到怀疑 - 如果我们立即覆盖它,那么提供这个参数的意义是什么?也许参数应该是 answer
而不是 userGuess
?
英文:
This loop structure probably isn't what you want. In having 2 while loops, you are not actually capping the number of guesses in any way - all you do is re-run the guessing program 6 times. Additionally, since that inner while loop executes until the answer is exactly right, the user has no way of knowing if their guess was too low or too high. You probably want something like this instead:
public static int guess(int userGuess){
do {
System.out.println("Enter your guess: ");
userGuess = scan.nextInt();
if (userGuess < answer) {
System.out.println("Too low, try again");
} else if (userGuess > answer) {
System.out.println("Too high, try again");
} else {
// todo tell the user they were right
}
numOfGuesses = numOfGuesses + 1;
} while (numOfGuesses <= 6 && userGuess != answer);
// in the case the user used up all their guesses, tell them they didn't get it right
// todo return something here because this function is supposed to return an int
}
Also note my comment at the bottom, this method won't compile until the method returns something. I am also assuming that all referenced variables are defined elsewhere.
I am also suspicious of the parameter being userGuess
- what would be the point of supplying a parameter we immediately overwrite? Perhaps the parameter is supposed to be answer
instead?
答案2
得分: 0
你程序中的一个主要问题是,while语句内部的条件不会给你想要的结果。你应该只使用一个while循环,而不是两个,条件应该是 while (numOfGuesses <= 6 && userGuess != answer)
。这是因为while循环会一直执行,直到它运行了六次或用户猜对答案为止。假设你正在使用一个类来存储实例变量和方法,另一个类包含主方法,你可以尝试使用以下方式。
public class Guess {
// 这些是你的实例变量
int numOfGuesses = 6;
int tempNum = 1;
// 答案可以是任何数字。我用2作为示例,这是要在6次尝试中猜出的数字。
int answer = 2;
public void guess(int userGuess, int answer) {
if (userGuess < answer) {
System.out.println("猜的数字太小了");
} else if (userGuess > answer) {
System.out.println("猜的数字太大了");
} else if (userGuess == answer) {
System.out.println("你猜对了答案是 " + answer);
System.exit(0); // 结束整个程序
}
}
}
上面的类包含了你的实例变量,但没有主方法和guess方法。因此,创建另一个带有主方法的类。这使用了面向对象编程。
import java.util.Scanner;
public class GuessTester {
public static void main(String[] args) {
// 初始化用户猜测
int userGuess = 0;
// 输入扫描器
Scanner input = new Scanner(System.in);
Guess guess = new Guess();
// 当Guess.tempNum <= Guess.numOfGuesses 并且 userGuess 不等于 Guess.answer 时,while循环变为false
while (guess.tempNum <= guess.numOfGuesses && userGuess != guess.answer) {
System.out.println("请输入一个猜测");
userGuess = input.nextInt();
guess.tempNum += 1;
guess.guess(userGuess, guess.answer);
// 如果临时数字等于猜测次数
if (guess.tempNum - 1 == guess.numOfGuesses) {
System.out.println("你的猜测次数用完了! ");
}
}
}
}
以上是你提供的代码的翻译。
英文:
One of the main problems in your program is that the conditions inside your while statements will not give you the outcome you want. Instead of two while loops you should use one instead with the conditions while(numOfGuesses<=6 && userGuess != answer). This is because the while loop will execute until it is run six times and if the user has not guessed the answer by then. Assuming you are using one class for your instance variables and methods, and another class which contains the main method you can try doing this instead.
public class guess {
//these are your instance variables
int numOfGuesses=6;
int tempNum =1;
//answer can be any number. I use 2 as an example, this is the number to be guessed in 6 tries.
int answer = 2;
public void guess(int userGuess, int answer){
if(userGuess < answer){
System.out.println("Too low of a number");
}
else if(userGuess>answer){
System.out.println("Too high of a number");
}
//do something when user guesses answer. Example, end the program.
else if(userGuess==answer) {
//print the correct guess
System.out.println("You have guessed the correct answer " + answer);
//exit out of entire program
System.exit(0);
}
}
}
The class above contains your instance variables but no main method and the guess method. So, create another class with a main method. This uses object oriented programming.
import java.util.Scanner;
public class guessTester {
public static void main(String[] args) {
//initialize userGuess
int userGuess = 0;
//input for scanner
Scanner input = new Scanner(System.in);
guess Guess = new guess();
//while loop becomes false when numOfGuesses = 6 and the userGuess does not equal to the answer.
while (Guess.tempNum<= Guess.numOfGuesses && userGuess != Guess.answer){
System.out.println("Enter a guess");
userGuess = input.nextInt();
Guess.tempNum+=1;
Guess.guess(userGuess,Guess.answer);
//if the temporary number is equal to number of guesses
if(Guess.tempNum -1 ==Guess.numOfGuesses) {
System.out.println("You ran out of guesses ! ");
}
}
}
}
答案3
得分: 0
我解决了。在我的项目被评分之后,我会发布完整的答案,但是需要使用一个for循环。
另外,我希望这能帮助未来在猜数字游戏项目中遇到困难的学生。显然不要直接复制粘贴,而是以此为起点。这是教授查看是否抄袭代码的首要位置。
private static int setGuess(int userGuess){
int differential = answer - guess;
String low = "太低了,请重试";
String high = "太高了,请重试";
String colder = "越来越冷";
String warmer = "越来越热";
System.out.println("请输入你的猜测:");
for(int i = 0; i < 6; i++){
while(userGuess != answer){
userGuess = scan.nextInt();
if (userGuess < answer){
System.out.println(low);
if(differential < 15){
System.out.println(colder);
}
else if (differential >= 15){
System.out.println(warmer);
}
else{
}
}
else if (userGuess > answer){
System.out.println(high);
if(differential <= 15){
System.out.println(warmer);
}
else if (differential > 15){
System.out.println(colder);
}
else{
}
}
else{
}
}
}
if (userGuess == answer){
System.out.println("恭喜你!你猜对了数字!");
}
if (numOfGuesses == MAX_GUESSES_ALLOWED){
gameOver = true;
}
return userGuess;
}
英文:
I figured it out. I'll post the whole answer after my project is graded, but it requires a for loop.
Also, I hope this helps future students who are having trouble with the guessing game project. Obviously don't copy and paste, but use this as a starting point. This is the first place professors look to see if you copied code.
private static int setGuess(int userGuess){
int differential = answer - guess;
String low = "Too low, try again ";
String high = "Too high, try again ";
String colder = "Getting colder";
String warmer = "Getting warmer";
System.out.println("Enter your guess.");
for(int i = 0; i < 6; i++){
while(userGuess != answer){
userGuess = scan.nextInt();
if (userGuess < answer){
System.out.println(low);
if(differential < 15){
System.out.println(colder);
}
else if (differential >= 15){
System.out.println(warmer);
}
else{
}
}
else if (userGuess > answer){
System.out.println(high);
if(differential <= 15){
System.out.println(warmer);
}
else if (differential > 15){
System.out.println(colder);
}
else{
}
}
else{
}
}
}
if (userGuess == answer){
System.out.println("Congratulations! You guessed the number!");
}
if (numOfGuesses == MAX_GUESSES_ALLOWED){
gameOver = true;
}
return userGuess;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论