0

我正在尝试使用主要是数组来编写此代码。我试图在每个问题之后显示“正确”或“不正确”,并在最后得到正确/不正确的答案。目标是使用数组来完成此任务,但我不知道如何使用数组在每个问题后显示正确/错误。

//Declarations
String rightAnswer[] = { "B", "C", "A" };
String userAnswer[] = new String [3];
String answer;
int counter;
int finalInt = 3;
int index = 0;
int correct = 0;
int incorrect = 0;

// detailedLoop
for(index = 0; index < finalInt; index++) {
    do {
        answer = JOptionPane.showInputDialog("What planet has the largest rings? \n\nA. Neptune \nB. Saturn \nC. Jupiter");
        userAnswer[index] = answer;
    } while(!(answer.equals("A")|| answer.equals("B") || answer.equals("C")));

    index++;
    do {    
        answer = JOptionPane.showInputDialog("What planet has a large redspot? \n\nA. Neptune \nB. Saturn \nC. Jupiter");
        userAnswer[index] = answer; 
    } while(!(answer.equals("A")|| answer.equals("B") || answer.equals("C")));

    index++;
    do {
        answer = JOptionPane.showInputDialog("What planet is farthest from the sun? \n\nA. Neptune \nB. Saturn \nC. Jupiter");
        userAnswer[index] = answer;
    } while(!(answer.equals("A")|| answer.equals("B") || answer.equals("C")));

    if(userAnswer[index].equals(rightAnswer[index])) {
        System.out.println("Correct!");
        correct++;
        index++;
    }
    if(!(userAnswer[index].equals(rightAnswer[index]))) {
        System.out.println("Incorrect. The right answer is " + rightAnswer[index] + ".");
        incorrect++;
        index++;
    }
}
System.out.println("Total correct:" + correct + ". Total incorrect: " + incorrect + ".");

任何帮助表示赞赏,谢谢。

4

5 回答 5

1

在遍历收到的答案之前,您永远不会将索引值重置为 0。因此,我相信您的程序正在尝试访问不存在的数组点,例如 3,4,5,因为您为仅 3 个字符串创建了一个数组

1 对您的程序进行修改以使其更加用户友好且更易于调试,这也是接受小写字母的答案。

此外,该程序不会告诉用户他们告诉他们的哪个问题是错误的或正确的,如果更改可以帮助您理解程序中的问题。

于 2012-04-09T01:31:15.497 回答
1

本节:

if(userAnswer[index].equals(rightAnswer[index]))
    {
    System.out.println("Correct!");
    correct++;
    index++;
    }
if(!(userAnswer[index].equals(rightAnswer[index])))
    {
    System.out.println("Incorrect. The right answer is " + rightAnswer[index] + ".");
    incorrect++;
    index++;
    }

需要处于某种 for 循环中。从技术上讲,它在 for 循环中,但是您正在增加index自己,因此它在 for 循环中无效

如果您执行以下操作,它应该可以工作:

int correct = 0;
int incorrect = 0;
for(i = 0; i < 3; i++) {
  if(userAnswer[i].equals(rightAnswer[i])) {
      System.out.println("Correct!");
      correct++;
    }   

  if(!(userAnswer[i].equals(rightAnswer[i]))) {
      System.out.println("Incorrect. The right answer is " + rightAnswer[i] + ".");
      incorrect++;
    }   
}

编辑

此外,作为一般规则,您不应该在 for 循环中增加计数器变量。for循环已经做到了。请注意我没有i在我的代码中增加自己。每次执行for循环时,i都会在i < 3评估之前递增。

于 2012-04-09T01:42:50.853 回答
1

似乎您在收集用户答案后没有重置索引,因此在您到达验证答案索引的块时,索引已经为 2,因此验证您的答案的代码仅检查数组的最后一个元素。基本上发生的事情是这样的:

  • 开始循环(索引从 0 开始)
  • 得到你的第一个答案(索引现在等于 1)
  • 得到你的第二个答案(索引现在等于 2)
  • 得到你的第三个答案(索引仍然等于 2)
  • index 的输出结果(index 还是 2,所以只处理最后一个答案)
  • 递增索引,返回循环开始(索引变为 3,终止循环)

循环的目的是遍历数组的索引,但每次收集答案后手动递增索引会导致意外行为。简单的解决方法是将测试您的答案的代码分成自己的循环,这样您就可以重新开始索引。如果这不能帮助您解决问题,我将在答案中添加代码。

于 2012-04-09T01:45:45.560 回答
1

这可能是你想要做的,再次,我不完全理解这个问题:

String questions[] = {
    "What planet has the largest rings? \n\nA. Neptune \nB. Saturn \nC. Jupiter",
    "What planet has a large redspot? \n\nA. Neptune \nB. Saturn \nC. Jupiter",
    "What planet is farthest from the sun? \n\nA. Neptune \nB. Saturn \nC. Jupiter"
};
String rightAnswer[] = { "B", "C", "A" };
String answer = null; // the user's answer
for (int i = 0; i < questions.length; i++) {
    do {
        answer = JOptionPane.showInputDialog(questions[i]);
    } while (!(answer.equals("A")|| answer.equals("B") || answer.equals("C")));
    String x = (answer.equals(rightAnswer[i])) ? "correct" : "incorrect.\nThe correct answer was " + rightAnswer[i];
    JOptionPane.showMessageDialog(null, "Your answer was " + x + ".");
}

请原谅我的风格。

于 2012-04-09T01:53:43.410 回答
0

我相信大多数早期的海报都回答了你并勾勒出(在其他答案中)类似的东西。自从我点击后,我就发布这个。

public class AstroQuiz {

    final public void run () {
        String[] questions = {
            "What planet has the largest rings? \n\nA. Neptune \nB. Saturn \nC. Jupiter",
            "What planet has a large redspot? \n\nA. Neptune \nB. Saturn \nC. Jupiter",
            "What planet is farthest from the sun? \n\nA. Neptune \nB. Saturn \nC. Jupiter"
        }
        String rightAnswers[] = { "B", "C", "A" };
        String validChoices[] = {"A", "B", "C"}

        int i = 0;
        int correct = 0;
        for (String question : questions) {
            String choice = askQuestionAndGetAnswer(question, validChoices);
            if(isCorrectAnswer(rightAnswers, i, choice)) {
                correct++;
                /* display "you got it!" here .. */
            } else {
                /* display "wrong answer!" here ... */
            }
            i++
        }
        System.out.format("Total correct:%d. Total incorrect: %d.\n", correct, questions.length - correct;)
    }

    final private String askQuestionAndGetAnswer(String question, String[] validAnswers) {
        String answer = null;
        boolean haveValidResponse = false;
        while (!haveValidResponse){
            answer = JOptionPane.showInputDialog(question);
            haveValidResponse = isValidResponse(answer, validAnswers)
        }
        return answer;
    }

    final private boolean isValidResponse(String response, String[] validAnswers) {
        for (String s : validAnswers) {
            if(response.equals(s))
                return true;
        }
        return false;
    }

    final private boolean isCorrectAnswer (String[] correctionAnswers, int questionIndex, String answer) {
        return correctionAnswers[questionIndex].equals(answer);     
    }
}
于 2012-04-09T02:16:17.077 回答