问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
import java.util.Scanner;
public class text1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] scores = new int [3];
Scanner scanner = new Scanner(System.in);
for(int i=0;i<scores.length;i++) {
System.out.print("请输入第"+(i+1)+"个评委的打分:");
scores[i] = scanner.nextInt();
}
scanner.close();
int temp,sum = 0;
for(int i = 0 ; i<scores.length; i++) {
sum += scores[i];
/*
在 sum 求和的过程中 数据总是比正确答案少 1
*/
for (int j = 0; j < scores.length-i-1; j++) {
if(scores[j] < scores[j+1]) {
temp = scores[j];
scores[j] = scores[j+1];
scores[j+1] = temp;
}
}
}
System.out.println("总分是:"+sum);
double result = (sum - scores[0] - scores[scores.length-1])*1.0 / (scores.length-2);
System.out.println("最高分:"+scores[0]);
System.out.println("最低分:"+scores[scores.length-1]);
System.out.println("平均分(去掉最高分、最低分):"+result);
}
}
运行结果及报错内容
请输入第1个评委的打分:2
请输入第2个评委的打分:3
请输入第3个评委的打分:4
总分数:8
最高分:4
最低分:2
平均分(去掉最高分、最低分):2.0