繁体   English   中英

在 java 中循环 arrays

[英]Looping with arrays in java

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

//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 + ".");

感谢您的帮助,谢谢。

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

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之前递增。

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

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

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

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

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 + ".");
}

请原谅我的风格。

我相信大多数早期的张贴者已经回答了你并勾勒出(在其他答案中)类似的东西。 我只是发布这个,因为我点击了。

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);     
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM