2017-02-12 9 views
0

Ich habe ein Problem mit meinem Code, wo das Programm nicht erneut abgespielt wird, aber wird auch nicht enden.Java Guessing Spiel Endlosschleife

Der Code wird in der inneren While-Schleife nach einem Gewinn oder zu vielen Vermutungen fortgesetzt.

Hier ist mein Code:

/** 
* This program will prompt the user to guess a secret number 
* This number will is between 1 and N, where N is a postive number 
* 
* @author: Perry Chapman 
*/ 

import java.util.Scanner; 

public class SecretNumber2 
{ 
    public static void main(String [] args) 
    { 
     int N; 
     int randomNumber;  
     int guess; 
     int tries, maxTries; 

     boolean win = false; 
     boolean playing = true; 
     int playAgain = 1; 
     tries = 0; 

     while(playing == true) 
     { 
      Scanner input = new Scanner(System.in); 

      System.out.println("This is a guessing game."); 
      System.out.println("What is the max number of tries: "); 
      maxTries = input.nextInt(); 
      System.out.println("Enter a value between 1 and 1000: "); 
      N = input.nextInt(); 

      randomNumber = (int)(N * Math.random()) + 1; 

      while (win == false) 
      { 
       System.out.println("Enter your guess: ");      
       guess = input.nextInt(); 
       tries++; 

       if (guess == randomNumber) 
       { 
        System.out.println("Congratulations! The number of guesses it took you was " + tries); 
        win = true; 
        System.out.println("Would you like to play again? \n (1)Yes or (2)No: "); 
        playAgain = input.nextInt(); 

        if(playAgain == 1) { 
         playing = true; 
        } 

        else if (playAgain == 2) { 
         playing = false; 
        } 
        tries = 0; 
       } 

       else if(guess < randomNumber) 
        System.out.println("Too low, guess again."); 

       else if(guess > randomNumber)       
        System.out.println("Too high, guess again."); 

       else if(tries == maxTries) 
       { 
        System.out.println("You have exceeded the max number of tries. \nYou lose."); 
        System.out.println("\nWould you like to play again?\n (1)Yes or (2)No: "); 
        playAgain = input.nextInt(); 

        if(playAgain == 1) { 
         playing = true; 
        } 

        else if(playAgain == 2) { 
         playing = false; 
        } 

        tries = 0; 
       } 
      } 
     } 
    } 
} 
+0

Warum fragen Sie "Was ist die maximale Anzahl der Versuche?" – Sedrick

+0

Frage klären –

Antwort

0

Ich glaube, Sie zwei Probleme.

Die erste ist, dass nach erfolgreichem Raten die Zahl, die Sie nie gesetzt haben, zurück zu false. Sie werden also nie ein zweites Mal die innere While-Schleife betreten.

Die zweite ist die Reihenfolge der if/else if-Anweisungen, es gibt nie ein Szenario, in dem eine Schätzung das finale else if erreichen kann. Jede Situation wird in einem der vorherigen drei Wenn bestätigt.

1

Andrew hat recht, um die Bedingung zu loopen muss wahr sein.Die if else-Anweisung muss in der richtigen Reihenfolge sein. Hier ist eine Lösung, ich hoffe, es wird helfen.

public static void main(String[] args) { 
    int N; 
    int randomNumber; 
    int guess; 
    int tries, maxTries; 

    boolean lose; 
    boolean playing = true; 
    int playAgain; 
    tries = 0; 

    while (playing) { 
     Scanner input = new Scanner(System.in); 

     System.out.println("This is a guessing game."); 
     System.out.println("What is the max number of tries: "); 
     maxTries = input.nextInt(); 
     System.out.println("Enter a value between 1 and 1000: "); 
     N = input.nextInt(); 

     randomNumber = (int) (N * Math.random()) + 1; 
     lose = true; 

     while (lose) { 
      System.out.println("Enter your guess: "); 
      guess = input.nextInt(); 
      tries++; 

      if (guess == randomNumber) { 
       System.out.println("Congratulations! The number of guesses it took you was " + tries); 
       lose = false; 
       System.out.println("Would you like to play again? \n (1)Yes or (2)No: "); 
       playAgain = input.nextInt(); 

       if (playAgain == 1) { 
        playing = true; 
       } else if (playAgain == 2) { 
        playing = false; 
       } 
       tries = 0; 
      } else if (tries == maxTries) { 
       System.out.println("You have exceeded the max number of tries. \nYou lose."); 
       System.out.println("\nWould you like to play again?\n (1)Yes or (2)No: "); 
       playAgain = input.nextInt(); 

       if (playAgain == 1) { 
        playing = true; 
       } else if (playAgain == 2) { 
        playing = false; 
       } 
       lose = false; 
       tries = 0; 
      } else if (guess < randomNumber) { 
       System.out.println("Too low, guess again."); 
      } else if (guess > randomNumber) { 
       System.out.println("Too high, guess again."); 
      } 
     } 
    } 
} 
0
/** 
    * This program will prompt the user to guess a secret number 
    * This number will is between 1 and N, where N is a postive number 
    * 
* @author: Perry C 
*/ 

import java.util.Scanner; 

public class SecretNumber 
{ 
public static void main(String [] args) 
{ 
    int N;  
    int guess; 
    int playAgain; 
    int tries; 
    int maxTries; 

    playAgain = 1; 
    tries = 0; 

    Scanner input = new Scanner(System.in); 

    while(playAgain == 1) 
     { 
     boolean win = false; 
     System.out.println("This is a guessing game."); 
     System.out.println("How many guesses would you like?"); 
     maxTries = input.nextInt(); 
     System.out.println("Enter a value between 1 and 1000: "); 
     N = input.nextInt(); 

     int randomNumber = (int)(N * Math.random()) + 1; 

    while (win == false) 
     { 
     System.out.println("Enter your guess: ");      
     guess = input.nextInt(); 
     tries++; 

     if(guess == randomNumber) 
     { 
      System.out.println("Congratulations! The number of guesses it took you was \t" + tries); 
      win = true; 
      System.out.println("Would you like to play again(1 or 2):"); 
      playAgain = input.nextInt(); 
      tries = 0; 
     } 

     else if(guess < randomNumber) 
      System.out.println("Too low, guess again."); 

     else if(guess > randomNumber)       
      System.out.println("Too high, guess again."); 

     if(tries == maxTries) 
     { 
      System.out.println("You have exceeded the max number of tries. \n You lose."); 
      System.out.println("Would you like to play again(1 or 2):"); 
      playAgain = input.nextInt(); 
      tries = 0; 
      win = true; 
     } 
     } 
    } 
} 
} 

das Problem gefunden, dachte ich die Antwort im Fall von jemandem würde sonst auf diesem Thread stolpern könnte! Danke für die Hilfe!