【问题标题】:Driver's license exam weird problems驾照考试怪问题
【发布时间】:2014-06-18 20:58:43
【问题描述】:

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

当地的驾照办公室要求您写信 对驾驶员的书面部分进行评分的程序 执照考试。考试有 20 道选择题。 以下是正确答案: 1. B 6. A 11. B 16. C 2. D 7. B 12. C 17. C 3. A 8. A 13. D 18. B 4. A 9. C 14. A 19. D 5. C 10. D 15. D 20. A

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

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

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”。

任何帮助将不胜感激。

【问题讨论】:

  • 不要认为你有不正确的问题。 20是A,19是D
  • questionsMissed中for循环的长度不要使用sizeArray,需要使用resp.length
  • 你试过调试你的代码吗?

标签: java arrays


【解决方案1】:

这是因为您在 questionsMissed 方法的循环中没有迭代足够多的时间:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多