grading a students test based on user input answer

2019-08-20 05:58发布

问题:

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.

回答1:

Here's a jump to a quick tutorial on arrays -> http://www.learn-java-tutorial.com/Arrays.cfm#.US09R-sjqy0

As stated in comments, we aren't going to write code for you, but I'll try to provide some helpful pointers:

Your going to want to prompt the user for how many students are in the class, this should have been taught in your class. Accept that input and store it in a variable.

Create a while loop to accept the data for each student. The loop should only run as many times as the user stated there were students.

In the loop accept all your student data, answers, names, etc.

After gathering all the data, you'll need to compare the students answer with the key. You can do this using a for loop or a while loop. I would recommend a for loop, create some kind of counter to keep up with how many answer were correct or incorrect, your choice then do the math after the loop to get your percentage.

Print the results and restart the loop.

Java Documentation on Arrays: http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Array.html

Some quick Array specific pointers:

Access an array:

String[] array = {"a", "b", "c", "d"}; //creates array
System.out.println( array[0] ); //prints out "a"
System.out.println( array.length ); //prints out "4", the number of elements in the array

Hope this helps!