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!
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.
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.");
}
Aucun commentaire:
Enregistrer un commentaire