217k views
0 votes
1. Write a program that grades arithmetic quizzes as follows: (4 points) a. Assume there are 23 questions in the quiz. b. Ask the user to enter the key (i.e., the correct answers). There should be one answer for each question, and each answer should be an integer. The numbers are entered on a single line, e.g., 34 7 13 100 81 3 9 10 321 12 34 7 13 100 81 3 9 10 321 12 9 10 321. c. Ask the user to enter the answers for the quiz to be graded. Store the answers in an array. d. Print the number correct and the percent correct. e. Put the whole program in a do-while loop.

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class Grade

{

public static void main(String[] args) {

final int LENGTH = 23;

int keys[] = new int[LENGTH];

int responses[] = new int[LENGTH];

int i = 1, correctCount = 0;

Scanner input = new Scanner(System.in);

do {

System.out.print("Enter the key for answer " + i + ": ");

int key = input.nextInt();

keys[i-1] = key;

System.out.print("Enter your response to question " + i + ": ");

int response = input.nextInt();

responses[i-1] = response;

if (keys[i-1] == responses[i-1]) {

correctCount ++;

}

i++;

}

while(i <= LENGTH);

double percentage = (double)correctCount / LENGTH;

System.out.println("Number of the correct answers: " + correctCount);

System.out.println("Percentage of the correct answers: " + percentage);

}

}

Step-by-step explanation:

- Initialize the variables

Inside the do-while loop :

- Ask for the answer key and put the values in keys array

- Ask for the responses and put the values in responses array

- If values at the same position in both arrays are equal, increase the correctCount, that means the response is correct

- Print the correcCount and percentage

User Shahzad Hassan
by
5.5k points