我是这里的新手,并且通常会进行编码。我要完成的工作是创建一个简单的猜谜游戏,提示用户输入数字,并对照计算机生成的1到100之间的数字检查该数字。我试图做到这一点,以便玩家可以继续猜测直到他们会得到正确的答案,并显示一个计数器,让玩家知道他们进行了多少次猜测。问题是,给出正确答案后程序不会终止,而且我无法弄清楚自己在做什么错。我将整个代码粘贴在底部以供参考,但是我觉得问题出在“ determineAnswer”方法的以下语句中:
} else if (userAnswer == computerNumber) {
message = "Correct"
+ "\nNumber of Guesses: " + count;
success++;
我试图使用整数“ success”的值作为终止do / while循环的条件,但是即使我尝试增加该值,循环也会继续进行,就好像该值正在不断重置一样。如果是这样,我看不到哪里出了问题。同样,我对此很陌生,但是我会很感激。
import javax.swing.JOptionPane;
public class GuessingGame {
public static void main(String[] args) {
// generate a random number from 1 to 100
int computerNumber = (int) (Math.random() * 100 + 1);
// declare other variables
int success = 0;
int count = 0;
// display the correct guess for testing purposes
System.out.println("The correct guess would be " + computerNumber);
// prompt user for a guess
do {
count++;
String response = JOptionPane.showInputDialog(null,
"Enter a guess between 1 and 100");
int userAnswer = Integer.parseInt(response);
// display result
JOptionPane.showMessageDialog(null, determineGuess(userAnswer, computerNumber, success, count));
} while (success == 0);
}
public static String determineGuess(int userAnswer, int computerNumber,int success, int count) {
String message = null;
if (userAnswer <= 0 || userAnswer > 100) {
message = "Invalid guess"
+ "\nNumber of Guesses: " + count;
} else if (userAnswer == computerNumber) {
message = "Correct"
+ "\nNumber of Guesses: " + count;
success++;
} else if (userAnswer > computerNumber) {
message = "Incorrect, Too High"
+ "\nNumber of Guesses: " + count;
} else if (userAnswer < computerNumber) {
message = "Incorrect, Too Low"
+ "\nNumber of Guesses: " + count;
}
return message;
}
}
答案 0 :(得分:2)
您不更新成功值,并且每次循环运行时,成功值都会为0,从而导致无限循环。
int success = 0;
int count = 0;
// display the correct guess for testing purposes
System.out.println("The correct guess would be " + computerNumber);
// prompt user for a guess
do {
count++;
String response = JOptionPane.showInputDialog(null,
"Enter a guess between 1 and 100");
int userAnswer = Integer.parseInt(response);
// display result
JOptionPane.showMessageDialog(null, determineGuess(userAnswer, computerNumber, success, count));
success=1;
} while (success == 0);
答案 1 :(得分:1)
在Java中,一切都是按值传递的。
在这种情况下,已将原语(int)传递给方法,然后更改其值并希望其值反映在调用方法中。 Java不能那样工作
public class SuccessTest {
public static void main(String[] args) {
int success = 0;
updateSuccess(success);
System.out.println(success); //will print 0
}
private static void updateSuccess(int success) {
//changing value of success here will not reflect in main method
success=2;
System.out.println(success);//will print 2
}
}
为了使这项工作声明成功为类级变量
private static int success = 0;
然后,如果您在 determineGuess 中更新成功的值,则无需将此成功传递给 determineGuess >方法,它将在主要方法中可用
答案 2 :(得分:0)
success
变量未在main
方法中进行更新的原因是,它无权访问determineGuess
方法的success
变量。它们是在不同范围中的两个独立变量。
determineGuess
接收success
作为int
方法参数。在Java中,诸如int
,char
和float
之类的类型是按值传递的:这意味着在将值赋给方法或设置为变量时,该值实际上是复制的,这样,如果您修改复制的值,则原始值不会被修改。 (实际上是all types are pass-by-value,但是对象的内容是通过引用提供的。)
有几种方法可以为success
方法更新main
变量,其中两种是:
success
设置一个字段,这样该类中的所有方法都可以访问它。因为您正在main
中进行所有操作,所以现在您需要success
是静态的:private static int success = 0;
。更好的方法是使所有内容均为非静态,并让main
实例化一个GuessingGame
对象,然后在其上调用一个run
方法。determineGuess
返回一个值,该值将使您知道答案适合的类别:成功,错误,过高或过低。然后,您将具有使用此输出选择要显示的消息的第二种方法。如果main
看到输出成功,它将更新其success
变量。这可能更好,但涉及更多。对于这个简单的示例,我建议使用选项1。实际上,由于仅检查一次成功,因此success
变量可以只是一个布尔值。这将使您的代码具有以下(经过修改):
import javax.swing.JOptionPane;
public class GuessingGame {
// Determines whether the user has successfully guessed the number
private static boolean success = false;
public static void main(String[] args) {
// Generate a random integer between 1 and 100 inclusive
int computerNumber = (int) (Math.random() * 100 + 1);
// Count the number of guesses that the user makes, to report it to them
int count = 0;
// FIXME: only for testing purposes, remove this
System.out.println("The correct guess would be " + computerNumber);
do {
count++;
String response = JOptionPane.showInputDialog(null, "Enter a guess between 1 and 100");
int userAnswer = Integer.parseInt(response);
JOptionPane.showMessageDialog(null, determineGuess(userAnswer, computerNumber, count));
} while (!success);
}
public static String determineGuess(int userAnswer, int computerNumber, int count) {
if (userAnswer <= 0 || userAnswer > 100) {
return "Invalid guess\nNumber of Guesses: " + count;
} else if (userAnswer == computerNumber) {
success = true;
return "Correct\nNumber of Guesses: " + count;
} else if (userAnswer > computerNumber) {
return "Incorrect, Too High\nNumber of Guesses: " + count;
} else if (userAnswer < computerNumber) {
return "Incorrect, Too Low\nNumber of Guesses: " + count;
}
return null;
}
}
如果要使用选项2,则您可能有一个enum GuessOutcome { INCORRECT, SUCCESS, TOO_LOW, TOO_HIGH }
从determineGuess
返回。然后,您将使用带有getOutcomeMessage(GuessOutcome outcome)
的{{1}}方法来选择要显示的消息。如果为switch (outcome) { ... }
,则为outcome == GuessOutcome.SUCCESS
。在此版本中,success = true
可以是success
的局部变量。