-2

I have been coding Java for a short period of time so I do not know why my System.exit(0) command is not working.

This program tests a user on 1st or 2nd grade math. It takes in two "random" numbers and the user answers arithmetic questions. When the program asks the user whether they want to play again, entering N for No does not seem to register with the if statement that contains System.exit(0). The program just restarts as if the user entered Y.

import java.util.*;


    class Tester {
        public static void main (String[] args) {

            SuperRandom randomNumberGenerator = new SuperRandom();

            Scanner scan = new Scanner (System.in);

            int selection, q1_1, q1_2, q1_3, q1_4, q1_5, questans;

            int score = 0;

            int a;

            int b, i;

            String scanchoice;

            String choice = "Y";

            final int numberOfQuestions = 5;

            int getNextRandom;

            int answer;

            while (choice == "Y"){

                System.out.println ("Welcome to Math Tester 2.0.");

                System.out.println ("");

                System.out.println ("Please enter your grade level:");

                System.out.println ("1. 1st Grade Test");

                System.out.println ("2. 2nd Grade Test");

                System.out.println ("3. Quit Math Tester 2.0");

                    selection = scan.nextInt();

                        if (selection == 1) {

                                System.out.println ("Enter: " +selection);

                                System.out.println ("");

                                for (i=1; i<=5; i++){

                                    a = randomNumberGenerator.getNextRandom(numberOfQuestions);

                                    b = randomNumberGenerator.getNextRandom(numberOfQuestions);

                                    System.out.println ("What is " +a + " + " +b + "?");

                                    answer = a+b;

                                    questans = scan.nextInt();

                                    System.out.println("" +questans);

                                        if (questans == answer){

                                            score++;        
                                        }

                                }   

                                System.out.println ("");

                                System.out.println ("Max points possible: 5.");

                                System.out.println ("Your score: " +score);

                                System.out.println ("Do you wish to play again (Enter Y for Yes or N for No)?");

                                    choice = scan.next();

                                        if (choice = "N") {

                                            System.out.println ("Goodbye!");

                                            System.exit(0); //Exits program if user entered N.
                                        }
                                        if (choice = "Y");

                                            choice = "Y";
                                        }   


                    if (selection == 2){

                        System.out.println ("Enter: " +selection);

                        System.out.println ("");    

                    }

                    if (selection == 3){

                        System.out.println ("Goodbye!");

                        System.exit(0);
                    }


            }

        }

    }
Prateek
  • 1,916
  • 1
  • 12
  • 22
user2873202
  • 1
  • 1
  • 2

2 Answers2

0

System.exit() is not executed in your code because your if/else conditions are not right. Use equals for string comparion instead of ==.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

As per the comments in Juned Ahsan'anwer you should change = to == or .equals() for example in this line if (choice = "N") { it should be if (choice.equals("N")) { Though == will not work in your case you should use .equals() = is an assignment operator where as == is a relational operator

SpringLearner
  • 13,738
  • 20
  • 78
  • 116