这是我正在为类进行的一个程序。编写大部分程序后,我现在试图运行它,似乎我有一个逻辑错误。出于某种原因,计算机只会执行直到第一个for循环,然后忽略scan.nextInt方法,我尝试提示用户将值输入到数组中。对于没有正确执行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);
}
2013-03-06
TommyD
+0
你确定你需要使用'new int [0]'(注意'numQuestions'是用0初始化的)吗? –
2013-03-06 02:52:14
+0
不客气。另外,下一次不要在标题中添加该语言,因为它已经在标签中。 –
2013-03-06 02:53:53