【发布时间】: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
-
你试过调试你的代码吗?