用最少的猜测来猜测游戏并不适用于Java

时间:2017-01-06 20:32:13

标签: java

我正在使用NetBeans在Java中创建一个猜谜游戏。猜谜游戏允许用户猜测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);

}

}

2 个答案:

答案 0 :(得分:0)

y从1开始,并且永远不会小于1,因此minGuess总是一个。

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

考虑仅在成功猜测时更新minGuess和maxGuess。

答案 1 :(得分:0)

您的if语句位于错误的位置。

你每次都在询问。如果用户没有猜到正确的号码也是如此。

所以只需将它放在你的numberguess方法中:

string s = "ᛡᚣ";