我已经在短时间内编写 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);
}
}
}
}