如何定义一个学生的类.

2024年11月18日 06:28
有1个网友回答
网友(1):

大略写了一个不知道合不合适:
public class Student {
private int id;
private String name;
private int sex;
private int age;
private int score1;
private int score2;
private int score3;
public Student(int id,String name,int sex,
int age,int score1,int score2,int score3)
{
this.id=id;
this.name=name;
this.sex=sex;
this.age=age;
this.score1=score1;
this.score2=score2;
this.score3=score3;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
}

public class TestStudent {
public static void main(String[] args) {
Student []stu=new Student[5];
stu[0]=new Student(1,"stu1",1,19,90,84,50);
stu[1]=new Student(2,"stu2",0,19,83,87,91);
stu[2]=new Student(3,"stu3",0,20,70,77,60);
stu[3]=new Student(4,"stu4",1,19,88,82,84);
stu[4]=new Student(5,"stu5",1,21,80,97,93);
int temp1=stu[0].getScore1();
int temp2=stu[0].getScore2();
int temp3=stu[0].getScore3();
int total1=temp1;
int total2=temp2;
int total3=temp3;
int maxScore1=temp1;
int maxScore2=temp2;
int maxScore3=temp3;
int minScore1=temp1;
int minScore2=temp2;
int minScore3=temp3;
for(int i=1;i<5;i++)
{
temp1=stu[i].getScore1();
temp2=stu[i].getScore2();
temp3=stu[i].getScore3();
total1+=temp1;
total2+=temp2;
total3+=temp3;
maxScore1=maxScore1>temp1?maxScore1:temp1;
maxScore2=maxScore2>temp2?maxScore2:temp2;
maxScore3=maxScore3>temp3?maxScore3:temp3;
minScore1=minScore1>temp1?temp1:minScore1;
minScore2=minScore2>temp2?temp2:minScore2;
minScore3=minScore3>temp3?temp3:minScore3;
}
System.out.println("average1 = "+(float)total1/5);
System.out.println("average2 = "+(float)total2/5);
System.out.println("average3 = "+(float)total3/5);
System.out.println("maxScore1 = "+maxScore1);
System.out.println("maxScore2 = "+maxScore2);
System.out.println("maxScore3 = "+maxScore3);
System.out.println("minScore1 = "+minScore1);
System.out.println("minScore2 = "+minScore2);
System.out.println("minScore3 = "+minScore3);
}
}