Java入门级程序 :Problem: Grading a Multiple-Choice Test

The problem is to write a program that grades multiple-choice tests. Suppose there are eight students and ten questions, and the answers are stored in a two-dimensional array. Each row records a student’s answers to the questions, as shown in the following array.

                                Java入门级程序 :Problem: Grading a Multiple-Choice Test_第1张图片

The key is stored in a one-dimensional array:

                    Java入门级程序 :Problem: Grading a Multiple-Choice Test_第2张图片

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]+"个");
	}
}

运行的结果如下:

Java入门级程序 :Problem: Grading a Multiple-Choice Test_第3张图片

你可能感兴趣的:(Java基础学习)