题目:
某某学校需要快速完成对于考试试卷的批改,因此需要设计出一个自动批改题目功能,以此来提升阅卷速度,每份试卷上有n道选择题/判断题,现在需要实现一个功能来完成对题目的批改,并进行打分。
功能要求:
1.学生人数、题数、标准答案均由用户提出;
2.标准答案和学生答案为String类型;
3.输出每个学生的得分;
4.输出得分最高的学生,输出该学生的分数。
代码如下:
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void start(){
Scanner scan = new Scanner(System.in);
System.out.println("========学生人数========");
int students = scan.nextInt();
System.out.println("========题目数量========");
int questions = scan.nextInt();
System.out.println("========标准答案========");
String correctAnswer = scan.next();
int[] scores = new int[students];
String[] answer = new String[students];
System.out.println("========学生答案========");
for (int i = 0; i < students; i++) {
int stuNumber = i + 1;
System.out.print(stuNumber + "号学生的答案为: ");
answer[i] = scan.next();
}
System.out.println("========得分统计========");
for (int i = 0; i < students; i++) {
int stuNumber = i + 1;
scores[i] = getScore(answer[i], correctAnswer);
System.out.println(stuNumber + "号学生的成绩为: " + scores[i]);
}
System.out.println("========第一名=========");
int[] bestStudent = findBestStu(scores);
if(bestStudent.length == 1){
System.out.println("第一名是: ");
printBestStu(bestStudent, scores);
} else{
System.out.println("有" + bestStudent.length + "个人并列第一");
System.out.println("分别是: ");
printBestStu(bestStudent, scores);
}
}
public static int getScore(String answer, String correctAnswer){
int score = 0;
for (int i = 0; i < answer.length(); i++) {
if(answer.charAt(i) == correctAnswer.charAt(i)){
score += 1;
}
}
return score;
}
public static int[] findBestStu(int[] scores){
int[] copyScore = Arrays.copyOf(scores, scores.length);
Arrays.sort(copyScore);
int count = 0;
for (int i = 0; i < scores.length; i++) {
if(scores[i] == copyScore[scores.length - 1]){
count++;
}
}
int[] arrayBestStu = new int[count];
int j = 0;
for (int i = 0; i < scores.length; i++) {
if(scores[i] == copyScore[scores.length - 1]){
arrayBestStu[j] = i;
j++;
}
}
return arrayBestStu;
}
public static void printBestStu(int[] bestStudent, int[] scores){
for (int x : bestStudent) {
int id = x + 1;
System.out.println(id + "号学生 成绩为: " + scores[x]);
}
}
public static void main(String[] args) {
start();
}
}
测试举例:
========学生人数========
6
========题目数量========
10
========标准答案========
ABCDAACCAA
========学生答案========
1号学生的答案为: ABCDAACCAA
2号学生的答案为: ABCCAACCAC
3号学生的答案为: ABCDAACCAA
4号学生的答案为: ABCCDDCCAC
5号学生的答案为: ABCDAACCAA
6号学生的答案为: ABCAADCCAD
========得分统计========
1号学生的成绩为: 10
2号学生的成绩为: 8
3号学生的成绩为: 10
4号学生的成绩为: 6
5号学生的成绩为: 10
6号学生的成绩为: 7
========第一名=========
有3个人并列第一
分别是:
1号学生 成绩为: 10
3号学生 成绩为: 10
5号学生 成绩为: 10