输入示例
4 3
A 30 30 30
B 40 40 40
c 50 50 50
D 60 60 60
输出示例
A 90.00 30.00
B 120.00 40.00
c 150.00 50.00
D 180.00 60.00
题目的意思,每一组求总和和平均数
import java.util.Scanner;
public class Student {
private String name;
private int[] scores;
// private Object[] score;
public String getName()
{
return name;
}
public Student(String name, int[] Scores) {
this.name = name;
scores=new int [Scores.length];//!!
for(int i=0;i<scores.length;i++)
scores[i]=Scores[i];
// 敲黑板,这里重点,必须重新开辟,不然会只会出现最后一组的结果。
}
public double getSum() {
double sum=0;
for(int i=0;i<scores.length;i++)
{
// sum=0;
sum+=scores[i];
}
return sum;
}
public double getAverage(){
double average=0;
average=getSum()/scores.length;
return average;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
// 学生数和科目数
int numberOfStudent = input.nextInt();
int numberOfCourse = input.nextInt();
Student[] stu = new Student[numberOfStudent];
String name;
int[] scores = new int[numberOfCourse];
// 构建同学和每个课的成绩
for (int i = 0; i < numberOfStudent; i++) {
name = input.next();
for (int j = 0; j < numberOfCourse; j++) {
scores[j] = input.nextInt();
}
stu[i] = new Student(name, scores);// 注意引用问题
}
// 按学生输出学生的总分和平均分
for (int i = 0; i < stu.length; i++) {
System.out.printf("%s %.2f %.2f\n", stu[i].getName(),
stu[i].getSum(), stu[i].getAverage());
}
}
input.close();
}
}