-2

我已经在短时间内编写 Java 代码,所以我不知道为什么我的System.exit(0)命令不起作用。

该程序测试用户一年级或二年级的数学。它接受两个“随机”数字,用户回答算术问题。当程序询问用户是否要再次播放时,输入 N 表示否似乎不会注册包含 System.exit(0) 的 if 语句。程序就像用户输入了 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);
                    }


            }

        }

    }
4

2 回答 2

0

根据 Juned Ahsan'anwer 中的评论,您应该更改=为,==或者.equals()例如在这一行中if (choice = "N") {,它应该是if (choice.equals("N")) { 虽然==在您的情况下不起作用,但您应该使用.equals() =赋值运算符,而==关系运算符也是

于 2013-10-12T05:04:56.097 回答
0

System.exit()未在您的代码中执行,因为您的if/else条件不正确。用于equals字符串比较而不是 ==。

于 2013-10-12T04:51:05.420 回答