0

How do I end my program?

I made 3 attempts for the user to answer a certain answer, but if the user incorrectly answered and used all the 3 attempts. How could I make the program end?

Here is my code:

while (attempts1-- > 0 && !answer1.equals(ans1))
        {
            System.out.print("What is your guess? ");
            ans1 = sc.next();
            
            if (ans1.equals(answer1)) {
                System.out.println("You guessed correctly!");
                attempts1 = 3;
            }
            else {
                System.out.println("Incorrect. Attempts remaining: " + attempts1 + " What is your guess again?");
            }
            if (attempts1 == 0) {
                System.out.println("Game over..");
            }
        }

After the game over, the program still continues to the next question even though the game is supposed to end. What's the code for it?

5 Answers 5

1

You needed to break. I have added that in if ( attempts1 == 0 ). Now it should fix your problem.

while( attempts1-- > 0 && !answer1.equals( ans1 ) ) {
            System.out.print( "What is your guess? " );
            ans1 = sc.next();

            if ( ans1.equals( answer1 ) ) {
                System.out.println( "You guessed correctly!" );
                attempts1 = 3;
            }
            else {
                System.out.println( "Incorrect. Attempts remaining: " + attempts1 + " What is your guess again?" );
            }
            if ( attempts1 == 0 ) {
                System.out.println( "Game over.." );
                break;
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

0

You could use

System.exit(0);

The 0 indicates the program exited gracefully, if there were an abnormal termination or something you could use 1 or another specific nonzero status code.

4 Comments

Any code after System.exit(0); will become unreachable
Is that not ok? The title of the question is "What code in java do I use to exit program or end program in CMD?"
Hmm, ok. I can't delete my answer since it is accepted. It should be fine though since OP is not creating an enterprise application, but just a small toy program regardless.
0

you are trying to check the value of input with correct answer in loop, so adding it to loop to check each time is redundand:

you can make the application to close after the correct answer found by using break keyword or 3 wrong attempt:

while (attempts1-- > 0) {
    System.out.print("What is your guess? ");
    ans1 = sc.next();
    
    if (ans1.equals(answer1)) {
        System.out.println("You guessed correctly!");
        break;
    } else {
        System.out.println("Incorrect. Attempts remaining: " + attempts1 + " What is your guess again?");
    }
    
    if (attempts1 == 0) {
        System.out.println("Game over..");
        System.exit(0);
    }
}

Comments

0

Based on the code you posted, I guess there is another bigger loop that loops through questions. So you need to stop the bigger loop too, with a flag. Something, like this:

for (String answer1: answers) {
        boolean gameOver = false;
        while (attempts1-- > 0 && !answer1.equals(ans1))
        {
            System.out.print("What is your guess? ");
            ans1 = sc.next();

            if (ans1.equals(answer1)) {
                System.out.println("You guessed correctly!");
                attempts1 = 3;
            }
            else {
                System.out.println("Incorrect. Attempts remaining: " + attempts1 + " What is your guess again?");
            }
            if (attempts1 == 0) {
                System.out.println("Game over..");
                gameOver = true;
            }
        }
        if (gameOver) {
            break;
        }
    }

Comments

0

There are a lot of suggestions to use System.exit(...) - which is technically the correct way to exit an application if there's a good reason to do so. However, in your case, you have a loop that you should exit and that's not working - which says that the problem is in your loop condition.

Anything else, be a it a break or a System.exit(...) call is just bypassing the fact that there's actually a problem somewhere else.

With that said - if I run your code exactly as it is, it behaves as expected. So what other code is around that that we can't see?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.