定义一个表示学生信息的类student,包括学号(NO)、姓名(Name)、性别(Sex)、成绩(Score)四个实例变量

2025-02-26 00:08:30
推荐回答(1个)
回答1:

public class Student
{

int NO;
String name;
String sex;
float score;
public Student(int nO, String name, String sex, float score)
{
super();
NO = nO;
this.name = name;
this.sex = sex;
this.score = score;
}
/**
* @return the score
*/
public float getScore()
{
return score;
}
/**
* @param score the score to set
*/
public void changeScore(float score)
{
this.score = score;
}
/**
* @return the nO
*/
public int getNO()
{
return NO;
}
/**
* @return the name
*/
public String getName()
{
return name;
}
/**
* @return the sex
*/
public String getSex()
{
return sex;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return "Student [NO=" + NO + ", name=" + name + ", sex=" + sex
+ ", score=" + score + "]";
}
}
定义两个Student对象
Student student1,student2;
student1.score=90;
student2.score=96;
//输出这两个学生的信息
system.out.println(student1.toString );
system.out.println(student2.toString );
//计算两人的平均值
float average_score = (student1.score +student2.score)/2;