7-3 编程练习题答案

时间:2023-02-22 14:20:29
import java.util.Scanner;

public class Test
{
	public static void main(String[] args)
	{
		int[][] answers = {
			{'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[] scores = getScore(keys, answers);
		showScore(scores);
	}

	public static int[] getScore(char[] keys, int[][] answers)
	{
		int[] scores = new int[answers.length];
		for(int i = 0; i < answers.length; i++)
		{
			for(int j = 0; j < answers[i].length; j++)
				if(answers[i][j] == keys[j])
					scores[i]++;
		}
		return scores;
	}

	public static void showScore(int[] scores)
	{
		int[] temp = new int[scores.length];
		System.arraycopy(scores, 0, temp, 0, scores.length);
		for(int i = 0; i < scores.length; i++)
		{
			int currentMaxScore = -1;
			int currentMaxIndex = -1;
			for(int j = 0; j < scores.length; j++)
			{
				if(scores[j] > currentMaxScore)
				{
					currentMaxScore = scores[j];
					currentMaxIndex = j;
				}
			}
			if(currentMaxIndex != -1)
			{
				System.out.println("Student " + currentMaxIndex + "'s correct count is " + scores[currentMaxIndex]);
				scores[currentMaxIndex] = -1;
			}
		}
	}
}