【发布时间】:2019-12-18 00:24:33
【问题描述】:
我正在尝试创建一个 Java 程序,用于对驾驶执照考试的书面部分进行评分,用户需要正确回答 20 个问题中的 15 个才能通过。应该有一种方法可以让用户输入他们的选择。包括输入验证,并且只接受字母 A、B、C 或 D。
还应该有第二种方法来评估学生的答案,显示正确回答的问题数量,学生是否通过了考试,并填充另一个数组来保存错误回答的问题编号。
第三种方法将显示错过的问题编号。
到目前为止我所拥有的:
import java.util.Scanner;
public class DriverLicenseExam
{
public static void main(String[] args)
{
// Arrays
String[] correctAnswers = {"B", "D", "A", "A", "C",
"A", "B", "A", "C", "D",
"B", "C", "D", "A", "D",
"C", "C", "B", "D", "A"};
String[] userAnswers;
int[] missed = new int[correctAnswers.length];
String[] answers = new String[20];
String answer;
// User's input
System.out.println("Enter your answers for the exam here: ");
Scanner input = new Scanner(System.in);
for (int x = 0; x < 20; x++)
{
do
{
System.out.print((x+1) + ": ");
answer = input.nextLine();
}
while (!isValidAnswer(answer));
answers[x] = answer;
}
//Process the user's answers
userAnswers = new String[answers.length];
for (int x = 0; x < answers.length; x++)
{
userAnswers[x] = answers[x];
}
//Results
int correctCount = totalCorrect();
int incorrectCount = totalIncorrect();
int missedCount = questionsMissed();
//Outputting total correct
System.out.println("Total Correct: " + correctCount);
//Outputting total incorrect
System.out.println("Total Incorrect: " + incorrectCount);
//Outputting missed questions
System.out.println("Total missed: " + missed);
}
// Determines total correct answers
public static int totalCorrect()
{
int correctCount = 0;
for (int x = 0; x < correctAnswers.length; x++)
{
if (userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
{
correctCount++;
}
}
return correctCount;
}
// Determines total incorrect answers
public static int totalIncorrect()
{
int incorrectCount = 0;
for (int x = 0; x < correctAnswers.length; x++)
{
if (!userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
{
missedCount[incorrectCount] = x;
incorrectCount++;
}
}
return incorrectCount;
}
// Determines total questions missed
public int[] questionsMissed()
{
return missedCount;
}
// Returns if user's answer is valid
public static boolean isValidAnswer (String answer)
{
return "A".equalsIgnoreCase(answer) ||
"B".equalsIgnoreCase(answer)
|| "C".equalsIgnoreCase(answer) ||
"D".equalsIgnoreCase(answer);
}
}
输出:
DriverLicenseExam.java:48: error: non-static method questionsMissed() cannot be referenced from a static context
int missedCount = questionsMissed();
^
DriverLicenseExam.java:48: error: incompatible types: int[] cannot be converted to int
int missedCount = questionsMissed();
^
DriverLicenseExam.java:65: error: cannot find symbol
for (int x = 0; x < correctAnswers.length; x++)
^
symbol: variable correctAnswers
location: class DriverLicenseExam
DriverLicenseExam.java:67: error: cannot find symbol
if (userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
^
symbol: variable correctAnswers
location: class DriverLicenseExam
DriverLicenseExam.java:67: error: cannot find symbol
if (userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
^
symbol: variable userAnswers
location: class DriverLicenseExam
DriverLicenseExam.java:80: error: cannot find symbol
for (int x = 0; x < correctAnswers.length; x++)
^
symbol: variable correctAnswers
location: class DriverLicenseExam
DriverLicenseExam.java:82: error: cannot find symbol
if (!userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
^
symbol: variable correctAnswers
location: class DriverLicenseExam
DriverLicenseExam.java:82: error: cannot find symbol
if (!userAnswers[x].equalsIgnoreCase(correctAnswers[x]))
^
symbol: variable userAnswers
location: class DriverLicenseExam
DriverLicenseExam.java:84: error: cannot find symbol
missed[incorrectCount] = x;
^
symbol: variable missed
location: class DriverLicenseExam
DriverLicenseExam.java:94: error: cannot find symbol
return missedCount;
^
symbol: variable missedCount
location: class DriverLicenseExam
10 errors
我遇到了这些错误,不知道如何解决。感谢您的帮助!
【问题讨论】:
-
目前
correctAnswers的范围仅限于main方法(因为那是您声明它的地方)。如果您想在其他方法中使用它,您需要将其声明为字段或将其作为参数传递给其他方法。 -
userAnswers也有同样的问题。 -
除了上面所说的之外,这里有一个通用的提示:在编码时,不要只是在运行代码之前先写3-4个函数。到那时,你已经积累了几十个错误,解开所有问题变得令人沮丧。如果你每改几行就编译运行代码,我想你会发现当你立即发现问题时,问题变得更加明显和可解决。