Java程序设计实用教程实验三3-38

Student类增加功能如下:

①学号以各专业,年纪分类自动编号

②实现以多条件进行查找,例如,只比较姓名

③增加成绩变量,按照专业的不同分别统计不同班级学生的成绩

public class Student1 {
	public String speciality;//专业
	public int number;//学号
	public int year,score;//入学年份,成绩
	private static int count=0;
	public String name;
    public String birthday;
    public String province;
    public String city;
	public Student1(String name,String birthday,String province,String city,
			String speciality,int year,int score)
	{
		this.set(name,birthday,province,city,speciality,year,score);
		count++;
	}
	public void set(String name,String birthday,String province,String city,String speciality,int year,int score)
	{
		this.name=name;
		this.birthday=birthday;
		this.province=province;
		this.city=city;
		this.speciality=speciality;
		this.year=year;
		this.score=score;
		
	}
	
	public void searchname(String nam) {
        int i = -1;
        i = name.indexOf(nam);
        if (i != -1)
            System.out.println(name);
    }
    public void searchbirthday(String num) {
        int i = -1;
        i = birthday.indexOf(num);
        if (i != -1)

            System.out.println(name);
    }
    public void searchplace(String place) {
        int i = -1;
        i = city.indexOf(place);
        if (i != -1)
            System.out.println(name);
    }
    public Student1(Student1 stu)
    {
        this(stu.name,stu.birthday,stu.province,stu.city,stu.speciality,stu.year,stu.score);
    }
    public String toString()
    {
        return "姓名:"+name+" 生日:"+birthday+" 省份:"+province+" 城市:"+city+" 专业:"+speciality+" 入学时间:"+year;
    }
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student1 stu[]=new Student1[5];
	    stu[0]=new Student1("张弈","1998年11月15日","青海","西宁","物联网1",2018,78);
	    stu[1]=new Student1("百灵","1999年5月12日","重庆","重庆","网络工程1",2018,67);
	    stu[2]=new Student1("王枫","1999年2月12日","山东","青岛","网络工程1",2018,89);
	    stu[3]=new Student1("王梓","1999年12月3日","甘肃","兰州","计算机2",2018,90);
	    stu[4]=new Student1("林州","2000年2月6日","山西","太原","计算机1",2017,80);
	    int acount=0;
		int bcount=0;
		int ccount=0;
		int dcount=0;
	    for(int i=0;i<=4;i++)
	    {
	        System.out.println(stu[i].toString());
			String a,b,c,d;
			a = new String("网络工程1");
	        b = new String("物联网1");
	        c = new String("计算机1"); 
	        d=new String("计算机2");
			if(stu[i].speciality.equals(a)) {
				acount+=1;
				stu[i].number = stu[i].year* 10000 + 01 * 100 + acount;
				System.out.println("学号"+stu[i].number);
			}
			if(stu[i].speciality.equals(b)) {
				bcount+=1;
				stu[i].number = stu[i].year* 10000 + 01 * 100 + bcount;
				System.out.println("学号"+stu[i].number);
			}
			if(stu[i].speciality.equals(c)) {
				ccount+=1;
				stu[i].number = stu[i].year* 10000 + 01 * 100 + ccount;
				System.out.println("学号"+stu[i].number);
			}
			if(stu[i].speciality.equals(d)) {
				dcount+=1;
				stu[i].number = stu[i].year* 10000 + 01 * 100 + ccount;
				System.out.println("学号"+stu[i].number);
			}
	    }
	    System.out.print("查找姓'王'的学生为:");
	    for(int i=0;i<=4;i++)
	    {
	        stu[i].searchname("王");
	    }
	    System.out.print("查找青岛市的学生为:");
	    for(int i=0;i<=4;i++)
	    {
	        stu[i].searchplace("青岛");
	    }
	    int sum1=0;
    	int sum2=0;
    	int sum3=0;
    	int sum4=0;

	    for(int i=0;i<=4;i++)
	    {
	    	String a, b, c,d;
	        a = new String("网络工程1");
	        b = new String("物联网1");
	        c = new String("计算机1");
	        d=new String("计算机2");
	    	if (stu[i].speciality.equals(a)) {
	            sum1+=stu[i].score;
	    	}
	    	
	    	if (stu[i].speciality.equals(b)) {
	            sum2+=stu[i].score;
	        }	    	
	    	if (stu[i].speciality.equals(c)) {
	            sum3+=stu[i].score;
	        }    	
	    	if (stu[i].speciality.equals(d)) {
	            sum4+=stu[i].score;
	        }
	    	
	    }
	    System.out.println("网络工程专业1的总成绩为:"+sum1);
    	System.out.println("物联网专业1的总成绩为:"+sum2);
    	System.out.println("计算机专业1的总成绩为:"+sum3);
    	System.out.println("计算机专业2的总成绩为:"+sum4);


	}

}

计算总成绩和学号我没有单独设立一个函数,而是将其放进了主函数里面。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值