【问题标题】:Why is my array not working as intended?为什么我的阵列没有按预期工作?
【发布时间】:2018-03-02 23:21:01
【问题描述】:

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

我的代码需要 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;
    }
}

【问题讨论】:

  • 您没有在调试器下逐步运行代码的任何原因?
  • 为什么循环是基于totalIncorrect
  • 为什么你只是在第 81 行粘贴了一个错误的整个代码?
  • @GalAbra 我认为整个代码对于理解该方法的作用是必要的。我本可以只发布该方法,但您不知道该方法做了什么或它调用了哪些其他方法。
  • 如果您不知道如何使用调试器,请在 someVariable 的可疑行为后添加 System.out.println(//someVariable//)...

标签: java arrays for-loop


【解决方案1】:

嗯,有一个简单的错误......你可以自己弄清楚为什么会这样......

提示:检查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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-29
    • 2019-08-28
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2021-01-03
    • 2018-11-08
    相关资源
    最近更新 更多