定义学生类用来存储学生的信息

***定义的学生类:包括属性、属性的getter、setter 方法

public class Student {//定义一个学生类,包括学号,姓名,三科成绩
	//属性和方法
	private String number;
	private String name;
	private int englishScore;
	private int mathScore;
	private int sportsScore;
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEnglishScore() {
		return englishScore;
	}
	public void setEnglishScore(int englishScore) {
		this.englishScore = englishScore;
	}
	public int getMathScore() {
		return mathScore;
	}
	public void setMathScore(int mathScore) {
		this.mathScore = mathScore;
	}
	public int getSportsScore() {
		return sportsScore;
	}
	public void setSportsScore(int sportsScore) {
		this.sportsScore = sportsScore;
	}
	public Student(String number, String name, int englishScore, int mathScore, int sportsScore) {
		super();
		this.number = number;
		this.name = name;
		this.englishScore = englishScore;
		this.mathScore = mathScore;
		this.sportsScore = sportsScore;
	}
	@Override
	public String toString() {
		return "Student [number=" + number + ", name=" + name + ", englishScore=" + englishScore + ", mathScore="
				+ mathScore + ", sportsScore=" + sportsScore + "]";
	}
}

2)查询并输出所有科目不及格的人数及名单。

public class TestStudent {
	private static Student[] stu;//声明对象数组
	public static void main(String[] args) {
		stu=new Student[5];//实例化对象数组
		stu[0]=new Student("111","aaaa",30,80,70);
		stu[1]=new Student("222","aabb",60,80,20);
		stu[2]=new Student("333","bbcc",40,80,70);
		stu[3]=new Student("444","ddee",30,30,70);
		stu[4]=new Student("555","ffgg",30,80,20);
		search("a");
		searchfail();
	}
	/*
	 * 根据姓名查找成绩
	 */
	public static void search(String name) {
		for(int i=0;i<stu.length;i++){
			//哪个学生的姓名中包括...
			if(stu[i].getName().contains(name)){
				System.out.println(stu[i]);
			}
		}
		
	}
	/*
	 * 查找成绩不及格的人数
	 */
	public static void searchfail() {
		System.out.println("英语的不及格有:");
		int a=0;
		for(int i=0;i<stu.length;i++){
			if(stu[i].getEnglishScore()<60){
				System.out.print(stu[i].getName()+",");
				a++;
			}
			
		}
		System.out.print(a);
		System.out.println();
		System.out.println("数学的不及格有:");
		int b=0;
		for(int i=0;i<stu.length;i++){
			if(stu[i].getMathScore()<60){
				System.out.print(stu[i].getName()+",");
				b++;
			}
		
		}
		System.out.print(b);
		System.out.println();
		
		System.out.println("体育的不及格有:");
		int c=0;
		for(int i=0;i<stu.length;i++){
			if(stu[i].getSportsScore()<60){
				System.out.print(stu[i].getName()+",");
				c++;
			}
			
		}
		System.out.print(c);
		System.out.println();
	}
	
}

(3)自己定义包含main方法的类,类中定义2个方法,分别用来实现按名字查询成绩、查找不及格人数并打印不及格名单的功能。。
public class TestChange {

public static void main(String[] args) {
	//实例化两个Swap对象
	Swap s1=new Swap(10);
	Swap s2=new Swap(100);
	swap(s1,s2);
	System.out.println(s1.getNum());
	System.out.println(s2.getNum());
}

public static void swap(Swap s1, Swap s2) {
	/*int temp=s1.getNum();
	s1.setNum(s2.getNum());
	s2.setNum(temp);*/
	Swap temp=s1;
	s1=s2;
	s2=temp;
	
}

}

public class Swap {
	private int num;
	//定义一个变量
	
	//setter,getter方法
	public int getNum() {
		return num;
	}

	public Swap(int num) {
		super();
		this.num = num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值