使用数组进行 Java 测验。需要带有评分答案的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29424984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making a quiz with Java using arrays. Need pointers with grading answers
提问by IAspireToBeGladOS
This is for a class. I'm having trouble figuring out how to best compare the user input with the array answerkey and consequently grade the answers given. I tried searching for a while but wasn't able to find what I needed, so any pointers would be much appreciated!
这是一个班级。我无法弄清楚如何最好地将用户输入与数组 answerkey 进行比较,从而对给出的答案进行评分。我尝试搜索了一段时间,但无法找到我需要的内容,因此非常感谢您的指点!
The prompt for the exercise is:
练习的提示是:
Write a DMV program that grades the written portion of the driver's license exam. It should have 20 multiple choice questions. It should ask the user to enter the student's answers for each of the 20 questions, which should be stored in another array. After the student's answer have been entered, the program should display a message indicating whether the student passed or failed the exam.( A student must correctly answer 15 of the 20 questions to pas the exam). It should then display the total number of correctly answered questions, and the total number of incorrectly answered questions. Input validation: Only accept the letters A, B, C or D.
编写一个 DMV 程序,对驾照考试的书面部分进行评分。它应该有20道选择题。它应该要求用户为 20 个问题中的每一个输入学生的答案,这些答案应该存储在另一个数组中。输入学生的答案后,程序应显示一条消息,指示学生是否通过了考试。(学生必须正确回答 20 道问题中的 15 道才能通过考试)。然后它应该显示正确回答问题的总数和错误回答问题的总数。输入验证:只接受字母 A、B、C 或 D。
My code so far:
到目前为止我的代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] answerkey = {"b","d","a","a","c","a","a","d","b","b","b","d","c","a","c","c","a","d","a","a"};
int n = 0;
int correct = 0;
int incorrect = 0;
String answer = "";
for (int i = 0; i < 20; i++){
System.out.println("Please enter your answers. Acceptable input is limited to A,B,C and D.\n");
answer = input.next();
if (answer.compareTo(answerkey[0])==0){
correct++;}
else {incorrect++;}
}
if (correct > 14){
System.out.println("You passed.");
} else {
System.out.println("You failed.");
}
System.out.println("You have " + correct + " correct answers.");
System.out.println("You have " + incorrect + " incorrect answers.");
}
回答by HopingToPassITCourse
Try this code on comparing the user input with the array myArray
.
尝试使用此代码将用户输入与数组进行比较myArray
。
Hope this helps.
希望这可以帮助。
String[] myArray= { //initialize values here
};
if ((myArray[0]).equals(answer)){
correct++;
} else{
incorrect++;
}
回答by mrfixij
Use a dynamic index accessing variable. Right now, every answer you have will be compared to the first answer ("b")
使用动态索引访问变量。现在,您的每个答案都将与第一个答案(“b”)进行比较
An example of this would be
这方面的一个例子是
String[] myArray = { //initialize values here
};
for (int index = 0; index <= myArray.length-1; index++){
if (answer.equals(myArray[index]){
correct++;}
else{
incorrect++;}
}