-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingerJudgingLine.java
More file actions
137 lines (116 loc) · 3.92 KB
/
Copy pathSingerJudgingLine.java
File metadata and controls
137 lines (116 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package exp2;
import java.util.Scanner;
public class SingerJudgingLine {
final static int JUDGEMENTS = 5; // 评委数
public static void main(String[] args) {
InputScore inputScore = new InputScore(JUDGEMENTS);
inputScore.inputScores();
float[] scores = inputScore.getScores();
System.out.print("[*] The scores before processing: ");
for (float score : scores) {
System.out.print(score + " ");
}
System.out.println();
DelScore delScore = new DelScore(scores);
delScore.delMaxMin();
ComputerAver computerAver = new ComputerAver(delScore.getProcessedScores());
float average = computerAver.computeAverage();
System.out.print("[*] The scores after processing: ");
for (float score : delScore.getProcessedScores()) {
System.out.print(score + " ");
}
System.out.println();
System.out.println("[*] The average score is: " + average);
}
}
class InputScore {
private Scanner scanner = new Scanner(System.in);
private float[] scores;
InputScore(int count) { // 评委数
this.scores = new float[count];
}
void inputScores() {
for (int i = 0; i < scores.length; i++) {
try {
System.out.print("[+] Please enter the score for judge " + (i + 1) + ": ");
scores[i] = scanner.nextFloat();
} catch (java.util.InputMismatchException e) {
System.out.println("[-] Invalid input. Please enter a valid number.");
scanner.nextLine();
i--;
} catch (Exception e) {
System.out.println("[-] An unexpected error occurred: " + e.getMessage());
scanner.nextLine();
i--;
}
}
System.out.println("[*] Scores have been entered.");
}
float[] getScores() {
return scores;
}
}
class DelScore {
private float[] scores;
private float[] processedScores;
class ScoresTooFewException extends RuntimeException {
public ScoresTooFewException(String message) {
super(message);
}
}
DelScore(float[] scores) {
this.scores = scores;
if (scores.length < 3) {
throw new ScoresTooFewException("[-] Not enough scores to remove max and min. Only " + scores.length + " scores provided.");
}
this.processedScores = new float[scores.length - 2]; // 去掉最大最小值后的数组
}
void delMaxMin() {
if (scores.length < 3) {
throw new ScoresTooFewException("[-] Not enough scores to remove max and min. Only " + scores.length + " scores provided.");
}
float max = scores[0];
float min = scores[0];
for (float score : scores) {
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
}
System.out.println("[*] Max score: " + max);
System.out.println("[*] Min score: " + min);
// 去掉最大最小值
int index = 0;
for (float score : scores) {
if (score != max && score != min) {
processedScores[index++] = score;
}
}
}
float[] getProcessedScores() {
return processedScores;
}
}
class ComputerAver {
private float[] scores;
class ScoresTooFewException extends RuntimeException {
public ScoresTooFewException(String message) {
super(message);
}
}
ComputerAver(float[] scores) {
this.scores = scores;
}
float computeAverage() {
if (scores.length == 0) {
throw new ScoresTooFewException("[-] No scores available to compute average.");
}
float sum = 0;
for (float score : scores) {
sum += score;
}
return sum / scores.length;
}
}