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?

Haru
  • 13
  • 4

5 Answers5

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;
            }
        }
Jabir
  • 2,776
  • 1
  • 22
  • 31
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.

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • Any code after System.exit(0); will become unreachable – Jabir Nov 18 '20 at 11:50
  • 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?" – Sash Sinha Nov 18 '20 at 11:51
  • Read this for details. https://stackoverflow.com/questions/3715967/when-should-we-call-system-exit-in-java?rq=1 – Jabir Nov 18 '20 at 11:55
  • 1
    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. – Sash Sinha Nov 18 '20 at 11:58
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);
    }
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
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;
        }
    }
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?

Riaan Nel
  • 2,425
  • 11
  • 18