因此,基本上,我被要求为此编写一个程序:
当地的驾照办公室要求您编写一个程序,对驾照考试的书面部分进行评分。该考试有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 18. B 4. A 9. 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”。
任何帮助将不胜感激。
这是因为您没有在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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句