publicclassStudent{privateStringid;//学号privateStringname;//姓名privateintengl;//英语成绩privateintmath;//数学成绩privateintcomp;//计算机成绩privatedoubleaver;//平均分pr...
public class Student {
private String id; // 学号
private String name; // 姓名
private int engl; // 英语成绩
private int math; // 数学成绩
private int comp; // 计算机成绩
private double aver; // 平均分
private Student students[]; // 学生
private int size; // 人数
Student(int size){
this.size=size;
};
Student(String id, String name, int engl, int math, int comp) {
this.id = id;
this.name = name;
this.engl = engl;
this.math = math;
this.comp = comp;
aver = average();
}
public Student(Student s){
this.id=s.id;
this.name=new String(s.name);
this.engl=s.engl;
this.math=s.math;
this.comp=s.comp;
this.aver=s.average();
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
for (int i = 0; i < size; i++)
this.students[i] = new Student(students[i]);
}
double average() {
return (engl + math + comp) / 3;
}
String getId() {
return id;
}
String getName() {
return name;
}
int getEnglish() {
return engl;
}
int getMath() {
return math;
}
int getComputer() {
return comp;
}
double getAverage() {
return aver;
}
public String toString() {
String s;
s = "学号" + "\t" + "姓名" + "\t" + "英语" + "\t" + "数学" + "\t" + "计算机"
+ "\t" + "平均分\n";
for (int i = 0; i
s = s + students[i].getId() + "\t" + students[i].getName() + "\t "
+ students[i].getEnglish() + "\t" + students[i].getMath()
+ "\t" + students[i].getComputer() + "\t"
+ students[i].getAverage() + "\n";
return s;
}
}
import java.util.*;
public class textStudent {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student students[];
students = new Student[5];
for (int i = 0; i < 5; i++) {
students[i] = new Student(getAStudent(i + 1));
}
Student stu = new Student(5);
stu.setStudents(students);
System.out.print(stu);
}
public static Student getAStudent(int i) {
Scanner Keyboard = new Scanner(System.in);
Student studenti;
System.out.println("输入第" + i + "个学生的信息:");
System.out.print("学号:");
String id = Keyboard.next();
System.out.print("姓名:");
String name = Keyboard.next();
System.out.print("英语成绩:");
int eng = Keyboard.nextInt();
System.out.print("数学成绩:");
int math = Keyboard.nextInt();
System.out.print("计算机成绩:");
int comp = Keyboard.nextInt();
studenti = new Student(id, name, eng, math, comp);
return studenti;
}
}
展开