class student{
private String name;
private int stuno;
private float english;
private float math;
private float computer;
private float average;
private float max;
private float sum; //类属性
public student(String n,int s,float e,float m,float c){ //构造方法,为属性赋值
this.setName(n);
this.setStuno(s);
this.setEnglish(e);
this.setMath(m);
this.setComputer(c);
}
public void setName(String n){
name = n;
}
public void setStuno(int s){
stuno = s;
}
public void setEnglish(float e){
english = e;
}
public void setMath(float m){
math = m;
}
public void setComputer(float c){
computer = c;
}
public String getName(){
return name;
}
public int getStuno(){
return stuno;
}
public float getEnglish(){
return english;
}
public float getMath(){
return math;
}
public float getComputer(){
return computer;
}
public void sum(){ //设置求和方法
sum = english + math + computer;
System.out.println("姓名:" + name + " 学号:" + stuno + 'n' + "总成绩=" + sum);
}
public void max(){ //设置求最大值方法
max = math;
max = max>english?max:english;
max = max>computer?max:computer;
System.out.println("最大值" + max);
}
public void average(){ //设置求平均值方法
sum = sum/3;
System.out.println("平均值:" + sum);
}
}
public class studentDemo{
public static void main(String args[]){
new student("张三", 24019563,80.7f, 90.4f, 84.5f).sum(); //使用匿名方法实例化对象,注意直接加方法进行调用
new student("张三", 24019563,80.7f, 90.4f, 84.5f).max(); //使用匿名方法实例化对象,注意直接加方法进行调用
new student("张三", 24019563,80.7f, 90.4f, 84.5f).average(); //使用匿名方法实例化对象,注意直接加方法进行调用
student stu1 = new student("李四",24019562 , 80.5f , 90.2f, 84.4f); //声明并实例化对象
stu1.sum();
stu1.average();
stu1.max();
}
}
姓名:张三 学号:24019563
总成绩=255.6
最大值90.4
平均值:0.0
姓名:李四 学号:24019562
总成绩=255.1
平均值:85.03333
最大值90.2
为什么输出值张三的平均值为0