【发布时间】:2016-07-23 09:20:43
【问题描述】:
我的任务是制作一个程序,其中包含一个 char 数组,其中填充了 10 个问题测试的正确答案。我制作了一个填充了用户答案的数组,并且应该使用测试答案检查用户答案,并在给定用户输入的情况下给出通过或失败的输出。代码编译,我可以输入 10 个字符,但是无论我输入什么字符,输出始终是 10 个答案不正确,共 10 个。
在过去的几个小时里,我一直在试图弄清楚这一点,并希望在这里得到一些帮助。
这里是sn-p的代码:
//Part 2
char[] correctAnswers = {'b', 'd', 'a', 'a', 'c', 'a', 'b', 'a', 'c', 'd'}; //Char arrays
char[] studentAnswers = new char[10];
System.out.println("What are the students 10 answers?"); //Getting student answers
for (int i = 0; i < correctAnswers.length; i++)
studentAnswers = scan.next().toCharArray();
int points = 0; //Used to calculate pass or fail
for (int i = 0; i < correctAnswers.length; i++);
int j = 0; //Used to identify each element in the array
if (correctAnswers[j] == studentAnswers[j]) //Checks each answer with the correct answers and adds 1 point if it is true
{
points++;
j++;
}
else {
j++;
}
if (points >= 8) {
System.out.println("Congratulations! \nYou have passed exam.");
System.out.println("Total number of correct answers: " + points); //print points
System.out.println("Total number of incorrect answers: " + (correctAnswers.length - points)); //10 - points would equal the remaining amount of points available which would be how many were missed.
} else {
System.out.println("Sorry, you have not passed the exam!");
System.out.println("Total number of correct answers: " + points);
System.out.println("Total number of incorrect answers: " + (correctAnswers.length - points));
}
【问题讨论】: