为什么我的程序会在用户输入" y"之后停止继续?

时间:2018-03-29 21:09:23

标签: java

import java.util.Scanner;
public class MathPractice {
public static void main(String[] args) {
    // Luke Mihalovich
    Scanner keyboard = new Scanner(System.in);
    int questions = getNumberOfQuestions();
    int difficulty = getQuestionDifficulty();
    int correct = 0;
    String set = "y,n";
    switch (set) {
    case "y": break;
    case "n": break; }
    if (difficulty == 1 || set.equals("y")) {
    for (int loop = 1; loop <= questions; loop++) {
    int random1 = (int)(9 * Math.random()) + 1;
    int random2 = (int)(9 * Math.random()) + 1;
    int randomSign = (int)(4 * Math.random()) + 1;
    int answerAdd; int rightAdd;
    int answerSub; int rightSub;
    int answerMul; int rightMul;
    int answerDiv; int rightDiv;
    if(randomSign == 1) {
        System.out.print("Question #" + loop + " What is " + random1 + " + " + random2 + "? ");
        answerAdd = keyboard.nextInt();
        rightAdd = random1 + random2;
        if (answerAdd == rightAdd) {
            System.out.println("Correct!");
            correct++;
        } else if (answerAdd != rightAdd) {
            System.out.println("Wrong... The answer is " + rightAdd); }
    }
    if(randomSign == 2) {
        System.out.print("Question #" + loop + " What is " + random1 + " - " + random2 + "? ");
        answerSub = keyboard.nextInt();
        rightSub = random1 - random2;
        if  (answerSub == rightSub) {
            System.out.println("Correct!");
            correct++;
        } else if (answerSub != rightSub) {
            System.out.println("Wrong... The answer is " + rightSub); }
    }
    if(randomSign == 3) {
        System.out.print("Question #" + loop + " What is " + random1 + " * " + random2 + "? ");
        answerMul = keyboard.nextInt();
        rightMul = random1 * random2;
        if  (answerMul == rightMul) {
            System.out.println("Correct!");
            correct++;
        } else if (answerMul != rightMul) {
            System.out.println("Wrong... The answer is " + rightMul); } 
    }
    if(randomSign == 4) {
        System.out.print("Question #" + loop + " What is " + random1 + " / " + random2 + "? ");
        answerDiv = keyboard.nextInt();
        rightDiv = random1 / random2;
        if  (answerDiv == rightDiv) {
            System.out.println("Correct!");
            correct++;
        } else if (answerDiv != rightDiv) {
            System.out.println("Wrong... The answer is " + rightDiv); }
    }
    }
}
    double score;
    score = (correct / (float) questions) * 100;
    System.out.print("You answered " + correct + " out of " + questions + " correctly (");
    System.out.printf("%.2f", score);
    System.out.println("%)");

    System.out.print("Would you like another set of questions? (y/n) ");
    set = keyboard.next();

我的程序询问用户他们想要问的随机生成的数学问题的数量。在询问并回答了指定数量的问题之后,程序然后通过输入&#34; y&#34;来询问用户是否想要另一组问题。更多,或&#34; n&#34;停止。目前,我的程序执行其他一切正常,除了当另一组问题的问题出现并且用户输入y以获得更多程序停止时。我怎样才能解决这个问题?感谢

1 个答案:

答案 0 :(得分:-1)

此程序仅在难度级别为1时运行,因为在语句

if (difficulty == 1 || set.equals("y"))

第二个条件永远不会满足,因为set包含“y,n”,它不等于“y”。

根据java API规范方法“java.lang.String.equals(Object anObject)”将此字符串与指定对象进行比较。当且仅当参数不为null并且是一个表示与此对象相同的字符序列的String对象时,结果才为真。

但是,您没有将用户输入用于下一组问题。 提出一个生成问题的方法

public void generateQuestions(int noq){
//all your code goes  here to generate noq number of questions
}

现在在主要方法中,在char类型变量中要求用户输入为Y或N将更容易,更快速地进行比较

int questions = getNumberOfQuestions();
int difficulty = getQuestionDifficulty();
char userPrompt=keyboard.next().charAt(0);
if(userPrompt=='Y' || userPrompt=='y' || difficulty == 1 )
generateQustions(questions);

如果你想使用switch语句,那么在一个案例之后调用genrateQuestions方法:'y'和break;