2014-10-17 49 views
0

我想写一个经典的初学者代码,我在那里做一个随机数,然后要求用户尝试猜测它。我试图通过使用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

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 
1

您有用户甚至检查猜测之前猜测的两倍,之前,你甚至进入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: ")); 
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循环,因为你希望他们做一个猜测:在while循环之前拆下第二猜测至少一次...

int numGuesses = 0; 
do { 
    numGuesses++; 
    guess = Integer.parseInt(JOptionPane.showInputDialog("Guess a number from 1-10: ")); 
} while (guess != random); 
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