Grumme
Grumme

Reputation: 806

Why does my program not stop?

Can anyone tell me, when I'm hitting y/n - why does my game not stop, it only starts again. I'm calling my isDone() method before the run, but it just runs over and over again and again.

public class Model {
    int numberToGuess = 29;
    int counter;
    boolean isDone;
    Scanner sc = new Scanner(System.in);

    public Model() {
        isDone = false;
        run();
    }

    public void run() {
        welcomeMessage();
        while (true) {
            guessNumber();
        }
    }

    public void welcomeMessage() {
        System.out.println("Welcome to " + '\n' + "***** GUESS THE NUMBER *****");
    }

    public void guessNumber() {
        System.out.println("Please enter number and hit 'Enter'" + '\n');
        if (sc.hasNextInt()) {
            int input = sc.nextInt();
            counter++;
            if (input < numberToGuess) {
                System.out.println('\n' + "Your guess is too low!" + '\n');
            } else if (input > numberToGuess) {
                System.out.println('\n' + "Your guess is too high!" + '\n');
            } else {
                System.out.println("Congratulations, you guessed the number!" + '\n' + "You guessed the number in " + counter + " guess." + '\n');
                isDone();
            }
        } else {
            System.out.println("Invalid input, please enter a number!" + '\n');
            sc.next();
        }
    }

    public void isDone() {
        System.out.println("Do you wanna play again? Enter y/n");
        if (sc.hasNext()) {
            String input = sc.next();
            if ("y".equals(sc))
                if (input.equals("y")) {
                    isDone = false;
                    guessNumber();
                } else if (input.equals("n")) {
                    isDone = true;
                }
        }
        System.out.println("Invalid input, please enter y/n to continue");
        sc.next();
    }
}

Upvotes: 2

Views: 118

Answers (4)

Sumon Mal
Sumon Mal

Reputation: 71

one small change you need to do

 public void run() {
    welcomeMessage();
    //while (true) {
      while (!isDone) {
        guessNumber();
    }
}

Upvotes: 0

Grumme
Grumme

Reputation: 806

Okay, now it's working :). The solution:

public class Model {

int numberToGuess = 29;
int counter;
boolean isDone;

Scanner sc = new Scanner(System.in);

public Model()
{
    isDone = false;
    run();
}

public void run()
{
    welcomeMessage();

    while (!isDone)
    {
        guessNumber();
    }
}

public void welcomeMessage()
{
    System.out.println("Welcome to " + '\n' + "***** GUESS THE NUMBER *****");
}

public void guessNumber()
{

    System.out.println("Please enter number and hit 'Enter'" + '\n');

    if (sc.hasNextInt())
    {
        int input = sc.nextInt();

        counter++;

        if (input < numberToGuess)
        {
            System.out.println('\n' + "Your guess is too low!" + '\n');

        }
        else if (input > numberToGuess)
        {
            System.out.println('\n' + "Your guess is too high!" + '\n');
        }
        else
        {
            System.out.println("Congratulations, you guessed the number!" + '\n' + "You guessed the number in " + counter + " guess." + '\n');
            isDone();
        }
    }
    else
    {
        System.out.println("Invalid input, please enter a number!" + '\n');
        sc.next();
    }
}

public void isDone()
{
    System.out.println("Do you wanna play again? Enter y/n");

    if (sc.hasNext())
    {
        String input = sc.next();

        if (input.equals("y"))
        {
          isDone = false;
          counter = 0;
        }

        else if (input.equals("n"))
        {

           isDone = true;
        }
        else
        {
            System.out.println("Invalid input");
            isDone();
        }
    }
}

Upvotes: -1

Ravindra babu
Ravindra babu

Reputation: 38950

isDone() method has logical flaw. I have corrected it.

Step 1: Change while (true) to while (!isDone)

Step 2: Corrected "y" condition for input and commented last two lines after making isDone=true, which is causing program to continue even after entering "n"

Modified code:

public void isDone() {
        System.out.println("Do you wanna play again? Enter y/n");

        if (sc.hasNext()) {
            String input = sc.next();
            if ("y".equals(input)) {
                isDone = false;
                guessNumber();
            } else if (input.equals("n")) {
                isDone = true;
            }
        }
        //System.out.println("Invalid input, please enter y/n to continue");
        //sc.next();
    }

EDIT: Explanation of flaw in while condition.

You are comparing scanner object with "y" instead of input value read from the scanner with "y"

Upvotes: 3

Darren
Darren

Reputation: 70776

You have an infinite loop:

 while (true)

Theres no condition to terminate the program.

You could use your global isDone variable and replace the true with:

while (!isDone)
{
   guessNumber();
}

Upvotes: 8

Related Questions