-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJudgesScore.java
More file actions
53 lines (48 loc) · 1.97 KB
/
JudgesScore.java
File metadata and controls
53 lines (48 loc) · 1.97 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
package Cases;
import java.util.Scanner;
public class JudgesScore {
/*in singing contest, many judges will score singers, score is integer between 0 and 100,
* singer final score is average score of subtracting highest and lowest score
* write a program to record judges' scores and calculate final score*/
/*analysis:
1.receiving data? number of judges
2.returning data? singer's final score
3.logic define: initiate an array, record scores to array, loop through scores
get highest and lowest score, return final score
* */
public static void main(String[] args) {
//target: complete judge score case
System.out.println("Current singer's score is: " + getAverageScore(6));
}
public static double getAverageScore(int number){
//1.Define a dynamically initialized array, responsible for later storing the judges’ scores.
int [] scores = new int[number];
//2. loop through each spot in array, record scores from each judge
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.println("please record the "+ (i + 1) +"th judge's score: ");
int score = sc.nextInt();
scores[i] = score;
}
//3. get highest and lowest score
int sum = 0; //to get total of scores
int max = scores[0]; // get highest score
int min = scores[0]; // get lowest score
//loop though array to get above data
for (int i = 0; i < scores.length; i++) {
int score = scores[i];
//get sum
sum += score;
// get max
if(score > max){
max = score;
}
//get min
if(score < min){
min = score;
}
}
//4.get average score and return
return 1.0 * (sum - min - max) / (number - 2);
}
}