我正在尝试编写一个经典的初学者代码,我在其中制作一个随机数,然后让用户尝试猜测。我试图通过使用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
你真的很接近!现在的情况是,在进入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
在检查猜测之前,甚至在进入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: "));
不要向用户询问两次随机值
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);
1)在进入while循环之前,您要求用户猜两次数字。由于numGuesses
设置为1,并在进入while循环时加1,因此您将偏离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