JAVA- How do I edit this code to calculate the number of correct answers for eac
ID: 3802449 • Letter: J
Question
JAVA- How do I edit this code to calculate the number of correct answers for each test (comparing the answers to the ANSWERKEY?
public class ExamResults{
public static void main(String[] args) {
String[][] results = {{"ANSWERKEY", "A", "D", "C"}, {"Dustin", "A", "D", "C"},
{"Eleven", "A", "C", "C"}, {"Hopper", "B", "B", "C"}, {"Mike", "A", "D", "A"}};
for (int i = 0; i < results.length; i++) {
for (int j = 0; j < results[i].length; j++) {
int count = 0
System.out.printf("%9s ", results[i][j]);
}
System.out.printf("%n");
}
}
}
Explanation / Answer
ExamResults.java
public class ExamResults{
public static void main(String[] args) {
String[][] results = {{"ANSWERKEY", "A", "D", "C"}, {"Dustin", "A", "D", "C"},
{"Eleven", "A", "C", "C"}, {"Hopper", "B", "B", "C"}, {"Mike", "A", "D", "A"}};
for (int i = 0; i < results.length; i++) {
int count = 0;
for (int j = 0; j < results[i].length; j++) {
if(results[0][j].equals(results[i][j]))
{
count++;
}
System.out.printf("%10s ", results[i][j]);
}
if(i==0)
System.out.print("No of Correct Ans");
if(i>0)
System.out.print(" "+count);
System.out.printf("%n");
}
}
}
________________
ANSWERKEY A D C No of Correct Ans
Dustin A D C 3
Eleven A C C 2
Hopper B B C 1
Mike A D A 2
___________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.