我的任务是创建一个具有char数组的程序,其中包含10个问题测试的正确答案。我制作了一个充满了用户答案的数组,并且应该根据测试答案检查用户的答案,并根据用户输入给出传递或失败的输出。代码编译,我可以输入10个字符,但无论我输入什么字符,输出总是10个答案不正确的10个。
在过去的几个小时里,我一直难以理解,并希望能在这里得到一些帮助。
以下是代码段:
//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));
}
答案 0 :(得分:0)
for(int i = 0; i < correctAnswers.length; i++)
studentAnswers = scan.next().toCharArray();
我认为你应该删除第一行。它出现两次。假设您将所有10个答案都读为1 String。
然后在任何地方使用i
代替j
,这是多余的(删除j
和j++
的声明,因此else
变得空的也可以删除。)
此外,您需要在{}
之后和输出之前(即在检查总分数之前)放置大括号for
。如果您在;
之后添加分号for
,则表示您没有做任何x次。
我知道你使用j
的原因:因为i
没有被定义。它只存在于循环中(由空,跳过或&#34;什么都不做&#34;命令:一个孤独的分号组成)。当您添加{}
(请参阅3.),然后i
就会存在于这些大括号之间。