I have to
- Declare/Initialize an array to hold the correct answers to a 12 question multiple choice quiz whose answers are: 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', ‘B’, ‘A’
- Read in the answers for a single student into another array using a for loop
- ‘Grade’ the quiz and display the following:
- Student Name
- Quiz Score as a percentage to 1 decimal (ex: 7 out of 12 would display 58.3%)
and then
- First, at the top of your program, ask the user how many students there are in the class
- Loop through and input each student’s answers, ‘grade’ quiz, and display the result for each student
- After all students have been processed, display the values of the high and low scores and the average quiz score as a percentage (1 decimal)
program should end up looking like:
How many students are in the class? 2
Enter name for Student 1: Bob
Enter quiz score answers: <allow user to input 12 answers>
Bob
66.7%
Enter name for Student 2: Fred
Enter quiz score answers: <allow user to input 12 answers>
Fred
91.7%
The high score is 11 and the low score is 8
Average is: 79.2%
I have this array set up but I really don't know how to implement it using the for loops.
import java.util.*;
public class Lab4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String name;
char [] answerKey= { 'B' , 'D' , 'A' , 'A' , 'C' , 'A' , 'B' , 'A' , 'C' , 'D' , 'B' , 'A' };
char [] userAnswers = new char[answerKey.length];
}
}
Thanks for the help so far guys, I've worked on it a bit more but its giving me an error after entering the answers this is what i have
import java.util.*;
import java.text.*;
public class Lab4 {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
String input;
int students;
int correctAnswers=0;
char [] answerKey= { 'B' , 'D' , 'A' , 'A' , 'C' , 'A' , 'B' , 'A' , 'C' , 'D' , 'B' , 'A' };
char [] userAnswers = new char[answerKey.length];
DecimalFormat df = new DecimalFormat("#0.0");
System.out.print("how many students are in your class?");
input = s.nextLine();
students=Integer.parseInt(input);
String [] name = new String[students];
int j=1;
while(students>=j)
{
System.out.print("Enter name of student" + j + ": ");
name[j] = s.nextLine();
System.out.print("Enter quiz score answers");
userAnswers[answerKey.length] = s.next().charAt(0);
for (int i = 0; i < userAnswers.length; ++i)
{
if(userAnswers[i]==answerKey[i]);
correctAnswers++;
}
System.out.print((df.format(correctAnswers/answerKey.length)) + "%");
j++;
}
}
}
The error message is Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12 at Lab4.main(Lab4.java:29)
I'm not sure what it means or how to fix it.