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);
}
}
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