所以,基本上,我被要求制作一个关于这个的节目:
当地的驾照办公室要求您编写一个程序,对驾照考试的书面部分进行评分。这次考试有20个选择题。以下是正确答案: 1. B. 6. A. 11. B 16. C.D. 7. B 12. C 17. C 3. A. 13. d 18. B 4. A. 9. C 14. A. 19. d 5. c 10. d 15. d 20
他们必须有至少15个正确的答案才能通过。我还被要求包括以下方法:- passed:如果学生通过了返回真
基本上,我做了这个节目:
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“这个问题。
任何帮助都将不胜感激。
发布于 2014-06-18 21:05:33
这是因为您没有在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;
}
}https://stackoverflow.com/questions/24294917
复制相似问题