驾驶执照考试奇怪的问题

时间:2014-06-18 20:58:44

标签: java arrays

所以,基本上我被要求制作一个关于此的程序:

当地驾驶执照办公室要求您写信 一个程序,对驱动程序的书面部分进行分级 执照考试。考试有20个多项选择题。 这是正确的答案: 1. B 6. A 11. B 16. C. 2. D 7. B 12. C 17. C. 3. A A. A 13. D 18. B. 4. A 14. C 14. A 19. D. 5. C 10. D 15. D 20. A

他们必须至少有15个正确答案才能通过。 我还被要求包括以下方法: - 传递:如果学生通过则返回true - totalCorrect:返回正确答案的总数 - totalIncorrect:返回错误答案的总数 - questionsMissed:返回包含学生错过的问题#的int数组。

基本上,我做了这个程序:

import hsa.*;
public class DriversLicense
{
    public static void main (String[] args)
    {
        char[] correct = {'B', 'D', 'A', 'A', 'C'
            , 'A', 'B', 'A', 'C', 'D'
            , 'B', 'C', 'D', 'A', 'D'
            , 'C', 'C', 'B', 'D', 'A'};

        char[] response = new char [20];
        int numCorrect;
        int numIncorrect;
        int[] QuestMissed;
        boolean passed;

        for (int a = 0 ; a < response.length ; a++)
        {
            Stdout.print ((a + 1) + ": ");
            response [a] = Stdin.readChar ();
            while (response [a] < 'A' || response [a] > 'D')
            {
                Stdout.println ("Invalid input. Enter answers with A, B, C, or D only!");
                response [a] = Stdin.readChar ();
            }
        }

        Stdout.println ();

        passed = examPassed (response, correct);
        numCorrect = totalCorrect (response, correct);
        numIncorrect = totalIncorrect (response, numCorrect);
        QuestMissed = questionsMissed (response, correct);

        if (passed)
            Stdout.print ("You passed!");
        else
            Stdout.print ("You didn't pass!");

        Stdout.println (" Below are the details of your marked exam: ");
        Stdout.println ("#s of correct questions: " + numCorrect);
        Stdout.println ("#s of incorrect questions: " + numIncorrect);

        if (QuestMissed.length > 0)
        {
            Stdout.print ("Questions missed: ");

            for (int a = 0 ; a < QuestMissed.length ; a++)
            {
                Stdout.print (QuestMissed [a]);
                Stdout.print (" ");
            }
        }
    }


    private static int totalCorrect (char[] resp, char[] ans)
    {
        int totalCor = 0;
        for (int a = 0 ; a < resp.length ; a++)
        {
            if (resp [a] == ans [a])
                totalCor++;
        }
        return totalCor;
    }


    private static int totalIncorrect (char[] resp, int right)
    {
        return (resp.length - right);
    }


    public static int[] questionsMissed (char[] resp, char[] ans)
    {
        int sizeArray = resp.length - totalCorrect (resp, ans);
        int[] missedQuestions = {};

        if (sizeArray < 1)
            return missedQuestions;

        else
        {
            missedQuestions = new int [sizeArray];
            int position = 0;
            for (int x = 0 ; x < sizeArray ; x++)
            {
                if (resp [x] != ans [x])
                {
                    missedQuestions [position] = (x + 1);
                    position = position + 1;
                }
            }
            return missedQuestions;
        }

    }


    private static boolean examPassed (char[] resp, char[] ans)
    {
        int cor;
        boolean flag = false;
        cor = totalCorrect (resp, ans);
        if (cor >= 15)
            flag = true;
        return flag;
    }
}

然而,遗憾的是,我没有按预期得到答案。

当我尝试将所有B输入答案时,除了错过的问题外,我把一切都弄好了:

You didn't pass! Below are the details of your marked exam: 
#s of correct questions: 4
#s of incorrect questions: 16
Questions missed: 2 3 4 5 6 8 9 10 12 13 14 15 16 0 0 0 

我不知道为什么在错过的问题中我得到“0 0 0”。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

这是因为你没有在questionsMissed方法的循环中迭代enought times:

public static int[] questionsMissed (char[] resp, char[] ans)
{
    int sizeArray = resp.length - totalCorrect (resp, ans);
    int[] missedQuestions = {};

    if (sizeArray < 1)
        return missedQuestions;

    else
    {
        missedQuestions = new int [sizeArray];
        int position = 0;
        for (int x = 0 ; x < sizeArray ; x++) /* HERE, you're not iterating through the whole array of questions/answers */
        {
            if (resp [x] != ans [x])
            {
                missedQuestions [position] = (x + 1);
                position = position + 1;
            }
        }
        return missedQuestions;
    }

}

因此,对于每个元素,初始化为0的数组不会完全填充错过的问题。

解决这个问题:

public static int[] questionsMissed (char[] resp, char[] ans)
{
    int sizeArray = resp.length - totalCorrect (resp, ans);
    int[] missedQuestions = {};

    if (sizeArray < 1)
        return missedQuestions;

    else
    {
        missedQuestions = new int [sizeArray];
        int position = 0;
        for (int x = 0 ; x < resp.length ; x++) /* Changed number of iterations */
        {
            if (resp [x] != ans [x])
            {
                missedQuestions [position] = (x + 1);
                position = position + 1;
            }
        }
        return missedQuestions;
    }

}