2017-01-06 4 views
0

Ich erstelle ein Ratespiel in Java mit NetBeans. Das Ratespiel ermöglicht es dem Benutzer, eine Zahl zwischen 1 und 10 zu erraten. In jeder Runde haben sie 5 Chancen, die Zahl zu erraten. Es gibt drei Runden im Spiel. Nachdem der Benutzer das Spiel beendet hat, werden die Statistiken mit der minimalen Anzahl von Raten und der maximalen Anzahl von Raten ausgegeben.Erraten Spiel mit minimalen Schätzungen verwendet nicht in Java arbeiten

Die minimale Rate funktioniert nicht und es gibt immer 1 aus. Im Moment habe ich das Programm so eingerichtet, dass es verfolgt, wie oft der Benutzer pro Runde rät. Nach jeder Runde vergleicht er diesen Wert mit dem minimalen Wert und dem maximalen Wert. Die minGuess ist auf 5 eingestellt, da es nicht möglich ist, mehr als 5 mal zu erraten. Die maxGuess wird auf 1 gesetzt, da sie immer einmal oder mehrmals raten.

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); 

} 

}

+0

Nur frage mich, ist nicht '((10-1) + 1)' ist nur '10'? – user2004685

+0

Der Teil der Zufallszahl funktioniert, es ist nur die minimale Anzahl von Schätzungen, die nicht funktioniert. –

+0

Ja. Es wird sicherlich. Ich habe mich nur gefragt, ob es einen Trick gibt, '10' so zu schreiben! :) – user2004685

Antwort

0

y beginnt an einem, und ist nie kleiner als eins, so minGuess immer man ist.

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

Betrachten Sie nur die Aktualisierung von minGuess und maxGuess nach einer erfolgreichen Schätzung.

+0

Danke für Ihre Hilfe. Ich werde es versuchen. –

+0

@Trebla, er beginnt bereits minGuess um 5. – Andreas

0

Ihre if-Anweisung ist am falschen Ort.

Sie fragen jedes Mal. auch wenn der Benutzer nicht die richtige Zahl rät.

so legte es nur in ur number Methode:

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; 
} 
+0

Danke für Ihre Hilfe. Ich werde es versuchen. –