为什么我的阵列没有按预期工作?

时间:2018-03-02 23:21:01

标签: java arrays for-loop

由于我的代码中存在最终的差异,因此无法提交作业。我花了几个小时试图解决它。

我的代码需要20个测试答案,可以是' A'' B',' C'或' D',然后告诉你是否通过了(> = 15正确)或失败,你有多少是正确的,以及你有多少错了。之后,它会告诉你错误的问题编号。

然而,只有前几个数字实际记录到我正在使用的数组中,而最后5个数字不是(它总是最后5个,不管你有多少错误)。奇怪的是,我使用for循环来读取数组中的值,因此不应存在差异。如果有的话,很可能会给我一个arrayIndexOutOfBoundsExeption

这里是代码,对问题的方法有一个评论(从第81行开始):

import java.util.Scanner;
public class ch7 {
    private static char[] correctAns = {
         'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 
         'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 
         'C', 'B', 'D', 'A'};

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        boolean pass;
        char[] answers = new char[20];

        for(int x = 0; x < answers.length; x++) {

            do {
                System.out.printf("Enter your answer for question %d:",
                                  x + 1);
                answers[x] = kb.next().charAt(0);
            }
            while(answers[x] != 'A' && answers[x] != 'B' &&
                  answers[x] != 'C' && answers[x] != 'D' && 
                  answers[x] != 'a' && answers[x] != 'b' && 
                  answers[x] != 'c' && answers[x] != 'd');
        }

        for(int x = 0; x < answers.length; x++) {
            answers[x] = Character.toUpperCase(answers[x]);
        }

        pass = passed(answers);
        if(pass == true)
            System.out.println("You Passed!");
        else
            System.out.println("You did not pass.");

        System.out.printf("You got %d answers correct out of 20.\n",
                          totalCorrect(answers));

        System.out.printf("You answered %d questions incorrectly.\n",
                          totalIncorrect(answers));

        int[] qMissed = questionsMissed(answers);

        if(qMissed.length != 0) {
            System.out.print("You got questions ");
            for(int x = 0; x < qMissed.length; x++) {

                if(x == qMissed.length - 1)
                    System.out.printf("and #%d ", qMissed[x]);
                else
                    System.out.printf("#%d, ", qMissed[x]);
            }
            System.out.println("wrong.");
        }
    }

    public static boolean passed(char[] ans) {
        int correct = 0;
        int incorrect = 0;
        for(int x = 0; x < ans.length; x++) {
            if(ans[x] == correctAns[x]) {
                correct++;
            }
            else
                incorrect++;
        }
        return correct > incorrect ? true: false;
    }

    public static int totalCorrect(char[] ans) {
        int correct = 0;
        for(int x = 0; x < ans.length; x++) {
            if(ans[x] == correctAns[x]) {
                correct++;
            }
        }
            return correct;
    }

    public static int totalIncorrect(char[] ans) {
        int incorrect = 0;
        for(int x = 0; x < ans.length; x++) {
            if(ans[x] != correctAns[x]) {
                incorrect++;
            }
        }
        return incorrect;
    }

    // problem somewhere in this method
    public static int[] questionsMissed(char[] ans) { 
        int nMissed = totalIncorrect(ans);
        int[] missedQuestions = new int[nMissed];
        int i = 0;
        for(int x = 0; x < totalIncorrect(ans); x++) {
            if(ans[x] != correctAns[x]) {
                    missedQuestions[i] = x + 1;
                    i++;
            }
        }
        return missedQuestions;
    }
}

1 个答案:

答案 0 :(得分:0)

嗯,有一个简单的错误......你可以自己弄明白为什么它应该是这样......

提示:检查for循环中的条件

    public static int[] questionsMissed(char[] ans) { 
        int nMissed = totalIncorrect(ans);
        int[] missedQuestions = new int[nMissed];
        int i = 0;
        for(int x = 0; x <ans.length; x++) {
            if(ans[x] != correctAns[x]) {
                    missedQuestions[i] = x + 1;
                    i++;
            }
        }
        return missedQuestions;
    }