(1)设计包含静态数据成员的Student类,在该类定义中包括:
数据成员:学号,年龄,分数score,及静态数据成员学生人数count;
定义成员函数用于设置和读取及显示学号,年龄,分数,累计学生人数;
静态成员函数getCount( ) 用于返回总人数;
外部函数average()用于求平均值。
(2)设计一个测试类Test在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的平均分。
麻烦各位了,谢谢大家。
用数组的语法定义就可以了呀,或者直接用 List 列表。参考:
import java.util.Scanner;
public class Student {
private String id;
private int age;
private double score;
public Student(String id, int age, double score) {
super();
this.id = id;
this.age = age;
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public static void main(String[] args) {
//定义数组
Student [] students = new Student[10];
Scanner in = new Scanner(System.in);
for(int i=0;i<students.length;i++) {
System.out.println("学号:");
String id = in.nextLine();
System.out.println("年龄:");
int age = in.nextInt();
System.out.println("分数:");
double score = in.nextDouble();
//数组元素赋值
Student student = new Student(id,age,score);
students[i] = student;
}
in.close();
}
}