Java实现一个学生类Student

  • 1、设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。

public class Student {
	
	public static void main(String[] args) {
		Student [] num =new Student [3];			//创建一个一维数组
		num[0] = new Student ("scc",112,123);		//为一维数组赋值
		num[1] = new Student ("sca",2018,1234);
		num[2] = new Student ("scb",2019,1235);
		
//		Student temp = new Student ();		//两种调用方法,使用这一种,需要在写一个无参的构造方法。一般都用下面的方法。
//		temp.swap(num);
		Student .swap(num);						//类名.方法名
		for(Student stu : num){			//foreach循环,for(元素类型t 元素变量x : 遍历对象obj)
			stu.show();
		}
	}
	
	private String name;
	private int number;
	private int score;
	
//	public String getName() {
//		return name;
//	}
//	public void setName(String name) {
//		this.name = name;
//	}
//	public int getId() {
//		return number;
//	}
//	public void setId(int id) {
//		this.number = number;
//	}
//	public int getScore() {
//		return score;
//	}
//	public void setScore(int score) {
//		this.score = score;
//	}
	public Student (){	
	}
	public Student (String name,int number,int score){
		//super();
		this.name = name;
		this.number = number;
		this.score = score;
	}
	
	public void show(){
		System.out.println("姓名:" + name + "学号: " + number + "成绩" + score);
	}
	
	public static void swap(Student [] stus){
		//String temp;									//这样定义的错误,是因为,在下面交换的是stus[j],而stus是StudentTrue来定义的所以,如下定义
		Student temp;							
		for(int i = 0;i < stus.length-1;i++){
			for(int j = 0;j < stus.length - i - 1;j++){
				if(stus[j].score > stus[j + 1].score){
					temp = stus[j];						//整个数组都变换,而不是仅仅变换成绩
					stus[j + 1] = stus[j];
					stus[j] = temp;
				}
			}
		}
	}
}



  • 20
    点赞
  • 128
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值