The key is stored in a one-dimensional array:
Your program grades the test and displays the result. It compares each student’s answers with the key, counts the number of correct answers, and displays it.
程序如下:
import java.util.Scanner;
public class GMCT
{
public static void main(String[] args)
{
char answer[][] =
{
{'A','B','A','C','C','D','E','E','A','D'},
{'D','B','A','B','C','A','E','E','A','D'},
{'E','D','D','A','C','B','E','E','A','D'},
{'C','B','A','E','D','C','E','E','A','D'},
{'A','B','D','C','C','D','E','E','A','D'},
{'B','B','E','C','C','D','E','E','A','D'},
{'B','B','A','C','C','D','E','E','A','D'},
{'E','B','E','C','C','D','E','E','A','D'}
};
char keys[] = {'D','B','D','C','C','D','A','E','A','D'};
int[] student = new int[answer.length];
for(int i = 0; i < answer.length; ++i)
{
int right = 0;
for(int j = 0; j < answer[i].length; ++j)
{
if(answer[i][j] == keys[j])
++right;
}
student[i] = right;
}
for(int i = 0; i < student.length; ++i)
System.out.println("student"+(i+1)+"个学生回答正确"+student[i]+"个");
}
}
运行的结果如下: