I keep getting an error and I don\'t know how to fix it. I got it running but th
ID: 3618634 • Letter: I
Question
I keep getting an error and I don't know how to fix it.I got it running but the output had a weird number on it and when Ifixed it this is the error I got:
DriversLicense.java:87: ';' expected
int[] missed = driver.questionsMissed());
^
1 error
//Driver
import java.util.Scanner;
public class DriversLicense
{
public static voidmain(String[] args)
{
// acceptkeyboard input
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your answers below. ");
char[]answers = new char[20];
// requestan answer for each question
for(int i =0; i < answers.length; i++)
{
// get input until input is valid
char input;
do
{
System.out.print(i + 1 + ". ");
// get character and make it upper case
input = Character.toUpperCase(keyboard.next().charAt(0));
}
while(input < 'A' || input >'D');
// store answer
answers[i] = input;
}
// printoutput here
DriverExamdriver = new DriverExam(answers) {};
System.out.println();
System.out.println("You "+(driver.passed()?"passed" : "did notpass") + ". ");
System.out.println("Correct: " + driver.totalCorrect() + " ");
System.out.println("Incorrect: " + driver.totalMissed() +" ");
System.out.println("Questions missed: " +driver.questionsMissed());
}
}
//class
class DriverExam
{
private char[] key = { 'B', 'D', 'A', 'A', 'C', 'A', 'B','A', 'C', 'D',
'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' };
private char[] answers;
public DriverExam(char[] ans)
{
answers = ans;
}
public boolean passed ()
{
return (totalCorrect() > 14);
}
public int totalCorrect()
{
int correct = 0;
for (int i = 0; i < key.length; i++)
{
if (key[i] == answers[i])
correct++;
}
return correct;
}
public int totalMissed()
{
int tmissed = 0;
tmissed = key.length -totalCorrect();
return tmissed;
}
public int[] questionsMissed()
{
int[] missed = driver.questionsMissed());
if(missed.length > 0) {
System.out.print("Questions missed: ");
for(int i = 0; i <missed.length; i++)
System.out.print(" " + missed[i]);
System.out.println();
}
}
}
Explanation / Answer
Accroding to your code, it won't allow the user to miss anyquestion. He has to answer the question at any cost. So there is no provision for missing any questions. But if youfeel, incorrect answers are the Missed questions then this codewill work fine. At the same time, there is one issue in this code.Missed questions count is not getting displayed properly. Somewiered number is getting displayed. so i corrected it. import java.util.Scanner; //class public class DriversLicense { public static voidmain(String[] args) { // acceptkeyboard input Scanner keyboard = new Scanner(System.in); System.out.println("Enter your answers below. "); char[]answers = new char[20]; // requestan answer for each question for(int i =0; i 'D'); // store answer answers[i] = input; } // printoutput here DriverExamdriver = new DriverExam(answers) {}; System.out.println(); System.out.println("You "+(driver.passed()?"passed" : "did notpass") + ". "); System.out.println("Correct: " + driver.totalCorrect() + " "); System.out.println("Incorrect: " + driver.totalMissed() +" "); System.out.println("Questions missed: " +((int[])driver.questionsMissed()).length); } } class DriverExam { private char[] key = { 'B', 'D', 'A', 'A', 'C', 'A', 'B','A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A' }; private char[] answers; public DriverExam(char[] ans) { answers = ans; } public boolean passed () { return (totalCorrect() > 14); } public int totalCorrect() { int correct = 0; for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.