ewbrowning
ewbrowning

Reputation: 107

Java: Restarting program after achieving certain value

I'm making a game which allows the user to guess the random number. In the event the user guesses correctly, and he/she decides to play again, a new number must be generated. I assumed that if I set boolean value "restart" to "true" at both the beginning (while making it false soon thereafter), and then setting it back to true after "restart" became true again, then the program would start over...it did not. Do you see what I'm doing wrong? Thanks.

  import java.util.*;
  import javax.swing.*;
  import java.util.Scanner;

  public class RandomGuess2
  {
  public static void main(String[] args)
  {
  Scanner input = new Scanner(System.in);

  String userNum;
  int userInput, numInt, genNum, amount = 0;
  int repeatPlay = 0;
  int x = 1;
  boolean numTruth, restart;
  boolean repeatIsYes = false;

  restart = true;
  userInput = JOptionPane.showConfirmDialog(null, "Would you like to try and guess the magic    
  number?", "Guessing Game", JOptionPane.YES_NO_OPTION);

  genNum = (1 + (int)(Math.random() * 1000));


  do
    if((numTruth = (userInput == JOptionPane.YES_OPTION)) || (repeatIsYes = (userInput == 
    JOptionPane.YES_OPTION)))
    restart = false;
    {
    userNum = JOptionPane.showInputDialog(null,"Enter the number you're thinking between 1 & 
    1000: ");
    numInt = Integer.parseInt(userNum);

    if((numInt > 1000) || (numInt < 1)) {
     JOptionPane.showMessageDialog(null, "Incorrect entry");
     repeatIsYes = true;
     }
     else if(numInt > genNum) {
     repeatIsYes = true;
     repeatPlay = JOptionPane.showConfirmDialog(null, "You guessed too high!" 
      + "\nWould you like to guess again?" + genNum);
     }
     else if(numInt < genNum) {
     repeatIsYes = true;
     repeatPlay = JOptionPane.showConfirmDialog(null, "You guessed too low!" 
      + "\nWould you like to guess again?" + genNum);
     }
     else if(numInt == genNum) {
     repeatIsYes = false;
     repeatPlay = JOptionPane.showConfirmDialog(null, "How did you do that? You guessed  
      + correctly!\nWould you like to guess again?" + genNum);
     if(restart = (repeatPlay == JOptionPane.YES_OPTION))
     {restart = true;}
     }

     if(repeatIsYes = (repeatPlay == JOptionPane.YES_OPTION)) {
     numTruth = true;
     repeatIsYes = true;
     }
     else {
     numTruth = false;
     repeatIsYes = false;
     JOptionPane.showMessageDialog(null, "Goodbye! \nYou played " + amount + " times.");
     break;
     }
    }
    else {
    numTruth = false;
    repeatIsYes = false;
    JOptionPane.showMessageDialog(null, "Goodbye!");
    break;
    }
  while(numTruth = true);      
  }}

Upvotes: 0

Views: 523

Answers (1)

Thomas
Thomas

Reputation: 88727

You never really use restart, i.e you only set it. To make your application start again check the value of restart at an appropriate place in your loop .

I don't have the time to thoroughly read your code (which, btw, is hard to read due to formatting and structure), but a simple solution would be to use two loops.

Here's some pseudocode:

while( playAnotherGame) {  //this could be your "restart" flag
  boolean gameIsRunning = true;

  while( gameIsRunning ) {
    //ask for user input
    //check guesses
    if( guessedCorrectly ) {
      gameIsRunning  = false; //game is finished

      //display result
      //ask for user input: restart or quit
      if( quit ) {
        playAnotherGame = false;
      }       
    }
    else {
      //ask for user input: guess again, new game or quit
      //handle user input 
    }
  }
}

Btw, constructs like this are error prone and hard to read:

//you set and check repeatIsYes at the same time
if(repeatIsYes = (repeatPlay == JOptionPane.YES_OPTION)) {
  numTruth = true;
  repeatIsYes = true; //this is already true, see the if-condition
}

Upvotes: 1

Related Questions