【问题标题】:Guessing game with minimum guesses used not working in Java使用最少猜测的猜测游戏在 Java 中不起作用
【发布时间】:2017-01-06 20:32:13
【问题描述】:

我正在使用 NetBeans 用 Ja​​va 创建一个猜谜游戏。猜谜游戏允许用户猜一个 1 到 10 之间的数字。每一轮他们有 5 次猜数字的机会。比赛共有三轮。用户完成游戏后,输出统计信息,包括最小猜测次数和最大猜测次数。

最小猜测不起作用,它总是输出 1。现在,我已经设置了程序,以便跟踪用户每轮猜测的次数。在每一轮之后,它将这个值与最小值和最大值进行比较。 minGuess 设置为 5,因为猜测次数不能超过 5 次。 maxGuess 设置为 1,因为他们总是会猜测一次或多次。

static void numberGuess(int guess, int randNum) {                              //creating a method to check if the user has guessed the correct number or if the guess should be higher or lower

if (guess < 0 | guess > 10) {
    System.out.println("Please enter a valid number between 1 and 10."); 
}
else if (guess == randNum) {
    System.out.println("You guessed the number correctly");
}
else if (guess < randNum) {
    System.out.println("Guess is too low");
}
else if (guess > randNum) {
    System.out.println("Guess is too high");
}




}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   /*Rational:  This program allows a user to guess a number between 1 and 10 five times per round. There are three rounds in one game.
                 The program then outputs the stats for the game. 
    */
   //declaration 
   int userGuess;            //creates a spot in memory for these variables 
   int numOfGuess = 0; 
   int invalidGuess = 0; 
   int minGuess = 5; 
   int maxGuess = 1; 
   int average; 

   Scanner Input = new Scanner (System.in);  //creates an object in the scanner clas
   //execution
   System.out.println("Welcome to Super Guessing Game! Guess a random number between 1 and 10. There are three rounds with one guess each.");
   loopOne:                                     //labels the loop as loopTwo
   for (int x = 1;  x <= 3; x= x + 1 ) {        //runs the loop for three rounds 
       System.out.println(" ");
       System.out.println("Round " + x);
       System.out.println("To exit the game at any point, enter a negative 1");
       System.out.println(" ");

       int randNum;
       randNum = 1 + (int)(Math.random() * ((10 - 1) + 1));     //generates the random number 


       loopTwo:                                        //labels the loop as loopTwo
       for (int y = 1; y <= 5; y= y + 1) {             //runs the loop five times (five guesses per round)
           numOfGuess = numOfGuess + 1;               //counts number of guesses user has made
           System.out.println("Guess " + y + " out of 5");
           System.out.println("Please guess a number between 1 and 10: ");
           userGuess = Input.nextInt();
           if (userGuess == -1){                       //sentinel to let the user quit at any time
           System.out.println("Thank you for playing");
           break loopOne;                             //breaks out of the loops if the user wants to stop playing
           }

           numberGuess(userGuess, randNum);      //calls the numberGuess method
           if (y < minGuess)                    //compares to see if the minimum number of guesses is less that the number of guesses the user has made this round
               minGuess = y; 
           if (y > maxGuess)                    //compares to see if the maximum number of guesses is greater than the number of guesses that the user has made this round 
               maxGuess = y; 


            if (userGuess <1 | userGuess > 10) {      //keeps track of invalid guesses
               invalidGuess = invalidGuess + 1; 
           }

           if (userGuess == randNum) {            //exits the round if the user guesses correctly
               break; 
           }
       }

   }
   average = numOfGuess / 3;              //calculates the average number of guesses
   System.out.println("Thanks for playing!");     //outputs the following 
   System.out.println("");
   System.out.println("Number of Guesses Made: " + numOfGuess); 
   System.out.println("Average Number of Guesses: " + average);  
   System.out.println("Number of Invalid Guesses: " + invalidGuess);
   System.out.println("Minimum Guesses Used: " + minGuess);
   System.out.println("Maximum Guesses Used: " + maxGuess);

}

}

【问题讨论】:

  • 只是想知道,((10 - 1) + 1) 不就是10吗?
  • 随机数部分起作用了,它只是最小的猜测数不起作用。
  • 是的。它肯定会的。我只是想知道这样写10是否有什么技巧! :)
  • 我相信如果你自己写 10,它会生成 0 到 9 之间的随机数。你必须在随机整数上加一。但是这样写 10 可以确保生成的数字在 1 到 10 之间。

标签: java


【解决方案1】:

y 从一开始,并且从不小于一,因此 minGuess 始终为一。

   for (int y = 1; y <= 5; y= y + 1) {             
   ...
       if (y < minGuess)                    
           minGuess = y; 
   ...
   }

考虑只在猜测成功后更新 minGuess 和 maxGuess。

【讨论】:

  • 感谢您的帮助。我试试看。
【解决方案2】:

你的 if 语句放错地方了。

您每次都在询问。如果用户没有猜对数字。

所以把它放在你的 numberguess 方法中:

else if (guess == randNum) {
 System.out.println("You guessed the number correctly");
 if (y < minGuess)                    //compares to see if the minimum number of guesses is less that the number of guesses the user has made this round
   minGuess = y; 
 if (y > maxGuess)                    //compares to see if the maximum number of guesses is greater than the number of guesses that the user has made this round 
   maxGuess = y; 
 }

【讨论】:

  • 感谢您的帮助。我试试看。
猜你喜欢
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
相关资源
最近更新 更多