首页 > 代码库 > 学生3门课的总分,平均分,最大值,最小值

学生3门课的总分,平均分,最大值,最小值

/***
 * 
 * @author Administrator
 *学生的成绩3门课 的总分,,平均分,,最大值
 *             最小值
 *
 *
 *
 *
 */
public class HomeWork {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s=new Student("小白",95f,78f,79.5f);
//		s.show();
//		s.sum();
//		s.avg();
		System.out.println(s.getName()+"的信息为:"+s.show());
		System.out.println(s.sum());
		System.out.println(s.avg());
		s.topScore();
		s.lowScore();
	}

}
/**
 * 学生类
 */
class Student{
	private String name;
	private float computer;//计算机成绩
	private float sports;//体育成绩
	private float music;//
	
	//getter/setter 方法,
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
	//构造方法
	public Student(){}//默认构造方法一般要保留Ctrl+D删除一行
	
	public Student(String name,float computer,float
			sports,float music){
		this.name=name;
		this.computer=computer;
		this.music=music;
		this.sports=sports;
	}
	//求总分
//	public void sum(){
//		float sum=computer+sports+music;
//		System.out.println(name+"的总成绩为:"+sum);
//	}
	public float sum(){
		float sum=computer+sports+music;
		return sum;
	}
	//求平均分
	public float avg(){
		float avg=sum()/3;
		//System.out.println(name+"的平均成绩为:"+avg);
		return avg;
	}
	//求最高分
	public void topScore(){
		//用三木运算符
		float max=computer>=sports?computer:sports;
		max=max>=music?max:music;
		System.out.println(name+"的最高成绩为:"+max);
	}
	//求最低分
	public void lowScore(){
		float min=computer<=sports?computer:sports;
		min=min<=music?min:music;
		System.out.println(name+"的最低成绩为:"+min);
	}
	//输出学生的全部信息
//	public void show(){
//		System.out.println("我是:"+name+"体育成绩为:"
//	+sports+"计算机成绩是:"+computer+"音乐成绩为:"+music);
//	}
	
	public String show(){
		return "我是:"+name+"体育成绩为:"
	+sports+"计算机成绩是:"+computer+"音乐成绩为:"+music;
	}
	
	
	
	
	
	
	
}

学生3门课的总分,平均分,最大值,最小值