我正在尝试编写经典的初学者代码,我在其中创建一个随机数,然后让用户尝试猜测它。我也尝试使用JOptionPane来获得一点想象力。一切正常,除了计算用户猜测随机数的次数的变量。它总是只有一个值低于应有的值(如果我在第三次尝试时得到它,它说我在两个中做到了正确)。我该如何解决?
第二个问题 - 我有out.println(randomNum);在我要求用户猜测数字之前(所以我可以作弊),但是在我猜到一次之后它就不会在控制台中打印出来。那是怎么回事?任何帮助将不胜感激!
int randomNum = new Random().nextInt(11); //define random number
int guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
int numGuesses = 1;
out.println(randomNum); //cheater
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
while (guess != randomNum) {
++numGuesses; //to increase the value of numGuesses by 1 each time the while loop iterates
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
}
JOptionPane.showMessageDialog(null, "You got it right after only " + numGuesses + " tries!");
out.println(numGuesses); //To see if it matches the JOptionPane value
答案 0 :(得分:2)
你真的很亲密!发生了什么事情是你在进入while
循环之前进行两次猜测,而不是递增numGuesses
。程序员经常试图避免重复代码。试试这个:
int randomNum = new Random().nextInt(11); //define random number
int guess = -1; // set it to an invalid value
int numGuesses = 0; // they haven't guessed at all at this point
out.println(randomNum); //cheater
while (guess != randomNum) {
++numGuesses; //to increase the value of numGuesses by 1 each time the while loop iterates
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
}
JOptionPane.showMessageDialog(null, "You got it right after only " + numGuesses + " tries!");
out.println(numGuesses); //To see if it matches the JOptionPane value
答案 1 :(得分:1)
在您进入while
循环之前,您甚至在检查猜测之前让用户猜两次。删除while
循环前的第二个猜测:
int guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
int numGuesses = 1;
System.out.println(randomNum); //cheater
// Remove the following line!
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
答案 2 :(得分:1)
不要让用户两次询问随机值
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
while (guess != randomNum) {
//...
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
相反,请使用do-while
循环,因为您希望它们至少猜一次......
int numGuesses = 0;
do {
numGuesses++;
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: "));
} while (guess != random);
答案 3 :(得分:0)
1)在进入while循环之前,您要求用户猜两次。由于numGuesses
设置为1并且在进入while循环时加1,因此您将逐一关闭(您要求用户在while循环中第三次猜测该数字)。
2)由于您在提示用户猜测数字后打印“骗子”代码,因此只会在猜测后出现。
请注意 Random().nextInt(11)
也包含0,因此您可能希望将guess
初始化为-1,如下例所示:
int randomNum = new Random().nextInt(11); //Define random number between 0 (inclusive) and 10 (inclusive)
int guess = -1;
int numGuesses = 0;
out.println(randomNum); //Cheater
while (guess != randomNum) {
guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 0-10: "));
numGuesses++; //To increase the value of numGuesses by 1 each time the while loop iterates
}
JOptionPane.showMessageDialog(null, "You got it right after only " + numGuesses + " tries!");
out.println(numGuesses); //To see if it matches the JOptionPane value