在我的JAVA课上给了一些功课,不得不写一个驾驶执照考试的程序。它必须有以下方法:
通过。如果学生通过考试,则返回true,或 如果学生失败,则为假
totalCorrect 。返回正确答案的总数 问题
totalIncorrect 。返回错误的总数 回答问题
questionsMissed 。包含问题编号的int数组 学生错过的问题
到目前为止,我已经做得很好,直到我找到错过的部分。我们应该记得不正确的更正,但是当我打电话给它时,我得到一个奇怪的数字[I @ 55f96302。我花了最后2个小时看这个节目,我不知道我搞砸了什么。任何帮助将不胜感激。谢谢
public class DLE {
private char[] key = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' };
private char[] answers;
public DLE(char[] ans)
{
answers = ans;
}
public boolean passed ()
{
return (totalCorrect() > 14);
}
public int totalCorrect()
{
int correct = 0;
for (int i = 0; i < key.length; i++)
{
if (key[i] == answers[i])
correct++;
}
return correct;
}
public int totalMissed()
{
int tmissed = 0;
tmissed = key.length - totalCorrect();
return tmissed;
}
public int[] questionsMissed()
{
int size = key.length - totalCorrect();
int[] missed = {};
if (size < 1)
return missed;
else
missed = new int [size];
int pos = 0;
for (int i = 0; i < key.length; i++)
{
if (key[i] != answers[i])
{
missed[pos] = (i + 1);
pos = pos + 1;
}
}
return missed;
}
}
//驱动程序
public class driver
{
public static void main(String[] args)
{
// accept keyboard input
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your answers below.\n");
char[] answers = new char[20];
// request an answer for each question
for(int i = 0; i < answers.length; i++)
{
// get input until input is valid
char input;
do
{
System.out.print(i + 1 + ". ");
// get character and make it upper case
input = Character.toUpperCase(keyboard.next().charAt(0));
}
while(input < 'A' || input >'D');
// store answer
answers[i] = input;
}
// print output here
DLE driver = new DLE(answers) {};
System.out.println();
System.out.println("You "+(driver.passed()?"passed" : "did not pass") + ".\n");
System.out.println("Correct: " + driver.totalCorrect() + "\n");
System.out.println("Incorrect: " + driver.totalMissed() + "\n");
System.out.println("Questions missed: " + driver.questionsMissed());
}
}
答案 0 :(得分:0)
使用Arrays.toString()实用程序方法打印整数数组。
import java.util.Arrays;
...
System.out.println(Arrays.toString(driver.questionsMissed()));