0
votes

Issue I have is with yes/no statement, if I enter "no" it will continue instead to exit the program. Please if someone can give me tip where issue is?

import java.util.Random; import java.util.Scanner;

public class NumberGame { private static final int DO_NOT_PLAY_AGAIN = 0;

private final Scanner mScanner;
private final Random mRandom;
private String mUserName;
private int mCorrectAnswer;
private int mPlayAgainInput;
private String mAnswer;

public NumberGame() {
    mScanner = new Scanner(System.in);
    mRandom = new Random();
}


public void run() {
    displayWelcomeMessage();
    getUserName();
    greetUser();
    getAnswer();


    do {
        intNumberGuessGame();
    } while (mPlayAgainInput != DO_NOT_PLAY_AGAIN);

    sayGoodbye();
}
  private void getAnswer(){
       System.out.println("Would you lioke to play a game enter yes to play or no to exit a game");
        mAnswer = mScanner.nextLine();
        if (mAnswer.equals("no")) 
        System.out.println("Maybe next time");
        sayGoodbye();
  }



private void displayWelcomeMessage() {
    System.out.println("Welcome to the game!");
    System.out.println("To play this game you have to"
                       + " guess a number and enter upon prompt or you can"
                       + " enter 0 to quit the game.");
}

private void getUserName() {
    System.out.println("Enter your user name: ");
    mUserName = mScanner.nextLine();
}

private void greetUser() {
    System.out.println("Let's play a game, " + mUserName + ".");
}

private void sayGoodbye() {
    System.out.println("Thanks for playing, " + mUserName + "!");
}

private void intNumberGuessGame() {
    // Get a random number between 1 - 100
    Random generator = new Random();
    mCorrectAnswer = mRandom.nextInt(100) + 1;
    int theirGuess = 0;
    int howManyTries = 0;
    while (theirGuess != mCorrectAnswer) {
        System.out.println("Guess my number: ");
        theirGuess = mScanner.nextInt();
        mCorrectAnswer = mRandom.nextInt(101) + 1;
        howManyTries++;

        System.out.println("Correct answer = " + mCorrectAnswer);

        if (theirGuess == mCorrectAnswer) {
            System.out.println("You guessed it! It only took you "
                    + howManyTries + " tries to get it right!");
            promptToPlayAgain();
            // They won the game, exit current loop
            break;
        } else if (theirGuess > mCorrectAnswer) {
            System.out.println("Your answer is too high.");
        } else if (theirGuess < mCorrectAnswer) {
            System.out.println("Your answer is too low.");
        }
    }
}

private void promptToPlayAgain() {
    System.out.println("Do you want to play again? (0 to quit): ");
    mPlayAgainInput = mScanner.nextInt();
}

}

public class MainApp {
public static void main(String[] args) {
    new NumberGame().run();
}
}
3
Your sentinel is zero, based on mPlayAgainInput = mScanner.nextInt();. Why are you assuming if it scans in "no" that it will exit the game? - BLaZuRE
this is part where I have problem, private void getAnswer(){ System.out.println("Would you lioke to play a game enter yes to play or no to exit a game"); mAnswer = mScanner.nextLine(); if (mAnswer.equals("no")) System.out.println("Maybe next time"); sayGoodbye(); } - DKCroat

3 Answers

0
votes

Your logic seems a little bit messy right now. I would advise you to redesign your code a little bit. Your problem lies in how getAnswer() function is designed - it prompts for answer, but does not use it. Change this function so it can return a boolean value:

private boolean getAnswer()
{
   System.out.println("Would you lioke to play a game enter yes to play or no to exit a game");
    mAnswer = mScanner.nextLine();

    if (mAnswer.equals("no"))
    {
        System.out.println("Maybe next time");
        sayGoodbye();
        return false;
    }

    return true;
}

Use this result in run() to check if game should be started at all:

public void run()
{
    displayWelcomeMessage();
    getUserName();
    greetUser();

    if(getAnswer()) //User wants to play!
    {
        do
        {
            intNumberGuessGame();
        } while (mPlayAgainInput != DO_NOT_PLAY_AGAIN);

        sayGoodbye();
    }
}
0
votes

First of all, when you're checking if mAnswer is equal to "no", you're only printing one string and you always execute the sayGoodbye because you're missing braces. It should be:

if (mAnswer.equals("no")) {
    System.out.println("Maybe next time");
    sayGoodbye();
}

Second of all, if you want the yes/no question to decide whether or not you continue playing, you will need to either check

do {
    intNumberGuessGame();
} while (mPlayAgainInput != DO_NOT_PLAY_AGAIN || !mAnswer.equals("no"));

or set the mPlayAgainInput value to 0 in the getAnswer() function, precisely in the braces i pointed out earlier. If you choose this option, you should change to do-while block to a regular while block.

0
votes

Because when the user inputs "no" you simply print a message and never actually tell the program to exit.

You can adjust it in many different ways, for example change the getAnswer method to:

    private int getAnswer() {
        System.out.println("Would you like to play a game? Enter yes to play or no to exit a game");
        mAnswer = mScanner.nextLine();
        if (mAnswer.equals("no")) {
            System.out.println("Maybe next time");
            return DO_NOT_PLAY_AGAIN;
        } else {
            return 1;
        }
    }

And your run method to:

    public void run() {
        displayWelcomeMessage();
        getUserName();
        greetUser();
        mPlayAgainInput = getAnswer();


        while (mPlayAgainInput != DO_NOT_PLAY_AGAIN) {
            intNumberGuessGame();
        }

        sayGoodbye();
    }