用JAV编写一个类,同时求出n个学生成绩的最高分、最低分、平均分以及超过平均分的人数,并编写主类完

如题所述

第1个回答  2011-05-27
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class C {

int[] scores;
int max = 0;
int min = 0;
double average = 0.0;
int overcount = 0;
int total = 0;

public C(){
try {
int n = getInput("请输入学生个数:");
scores = new int[n];
for(int i = 0;i<scores.length;i++){
scores[i] = getInput("请输入第"+(i+1)+"个学生的成绩");
}

} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

getResult(scores);
}

public Integer getInput(String name) throws NumberFormatException, IOException{

System.out.println("请输入"+name+":");
int n = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
return n;
}

//分析方法
public void getResult(int[] scores){

min = scores[0];
for(int i = 0;i<scores.length;i++){
max = Math.max(max, scores[i]);
min = Math.min(min, scores[i]);
total += scores[i];
}
average = total/scores.length;
for(int j = 0 ;j<scores.length;j++){
if(scores[j]>average){
overcount++;
}
}
System.out.println("最高分:\t"+max);
System.out.println("最低分:\t"+min);
System.out.println("平均分:\t"+average);
System.out.println("超过平均分的人数:\t"+overcount+"个人");
System.out.println("总分:\t"+total);
}

public static void main(String[] args){
new C();
}
}本回答被提问者采纳
相似回答