这是我正在为课堂准备的一个程序。在编写了大部分程序之后,我现在正在尝试运行它,看来我有一个逻辑错误。由于某种原因,计算机将一直执行到第一个 for 循环,然后忽略我尝试提示用户将值输入数组的 scan.nextInt 方法。
编辑:新问题:现在,当我运行程序时,未正确执行的循环是第二个 for 循环。无论您输入什么,它都会返回正确的总数作为可能的正确总数。也返回 0% 正确:(
编辑 2:我解决了正确百分比部分的问题,但我仍然不知道正确的方法来比较在为测验评分时输入的正确答案的输入值......它总是返回正确的数字是问题的总数,无论它们是否正确。
import java.util.Scanner;
public class gradingQuizzes {
public static void main(String[] args)
{
int numQuestions; // number of questions on quiz
int numCorrect = 0; // number correct of entered values
int correctAnswer;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the amount of questions in the Quiz: ");
numQuestions = scan.nextInt();
int[] key = new int[numQuestions]; // creates an array for the key to the quiz
// Prompts the user to enter values into the key array
for (int i = 0; i < key.length; i++)
{
System.out.println("Please enter the correct answer to question number " + (i + 1) + " : ");
key[i] = scan.nextInt();
}
System.out.println("Please enter the answers for the quiz to be graded: ");
System.out.println("---------------------------------------------------");
// 'for' loop asking for the correct answer for each question on the quiz
for (int i = 0; i < key.length; i++)
{
System.out.println("Question " + (i + 1) + " answer: ");
correctAnswer = scan.nextInt();
if (correctAnswer == key [i]);
{
numCorrect++; // increments the number correct for each match to the key
}
}
// Creates a variable to compute the percent correct on the quiz
double percentCorrect = (numCorrect / (key.length));
System.out.println("The number correct is: " + numCorrect);
System.out.println("The percent correct: %" + percentCorrect);
}