用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

149-教师类2

2018-10-13 作者: 芙蓉妹妹举报

[java]代码库

import java.util.Scanner;
import java.util.Arrays;
public class Main {
	public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);    
 
        int num = scan.nextInt();
        Teacher[] t = new Teacher[num];
        TeacherManagement tea = new TeacherManagement(num);
        for(int i = 0;i < num; i++)
        {
        	int no = scan.nextInt();
        	String name = scan.next();
            int age = scan.nextInt();
            String se = scan.next();
            t[i] = new Teacher(no,name,age,se);
            tea.add(t[i]);
        }
        Arrays.sort(t);
        for( int i = 0; i < num; i++)
        	System.out.println(t[i].toString());
        String name = scan.next();
        tea.search(name);
        num = scan.nextInt();
        tea.search(num);
        
        scan.close();
   }
}
class Teacher implements Comparable
{  
       int no;
       int age;
       String name;
       String seminary;
       public Teacher(int n, String na, int a, String s)
       {
    	   no = n;
    	   name = na;
    	   age = a;
    	   seminary = s;
       }
       void setno(int n)
       {
    	   no = n;
       }
       void setname(String n)
       {
    	   name = n;
       }
       void setage(int a)
       {
    	   age = a;
       }
       void setseminary(String s)
       {
    	   seminary = s;
       }
       int getno()
       {
    	   return no;
       }
       int getage()
       {
    	   return age;
       }
       String getname()
       {
    	   return name;
       }
       String getseminary()
       {
    	   return seminary;
       }
       boolean equals(Teacher t)
       {
    	   if(this.no == t.getno())
    		   return true;
    	   else 
    		   return false;
       }
       public String toString()
       {
    	   return "no: "+no+", name: "+name+", age: "+age+", seminary: "+seminary;
       }
       @Override//重写
       public int compareTo(Object a)
       {
    	   Teacher b = (Teacher) a;
    	   if(this.no > b.no)
    		   return 1;
    	   else if(this.no < b.no)
    		   return -1;
    	   else 
    		   return 0;
       }
}
class TeacherManagement
{
	 private static int num = 0; 
	Teacher[] teacher ;
	 public TeacherManagement(int a)
	 {
		 teacher = new Teacher[a];
	 }
	 void add(Teacher t)
	 {
		 teacher[num] = t;
		 num++;
	 }
	 void search(String n)
	 {
		 int flag = 0;
		 System.out.println("search by name:");
		 for(int i = 0; i < num; i++)
		 {
			 if(n.equals(teacher[i].getname()))
			 {
				 System.out.println(teacher[i].toString());
				 flag = 1;
			 }
		 }
		 if(flag == 0)
			 System.out.println("no such teacher");
	 }
	 void search(int a)
	 {
		 int flag = 0;
		 System.out.println("search by age:");
		 for(int i = 0; i < num; i++)
		 {
			 if(a == teacher[i].getage())
			 {
				 System.out.println(teacher[i].toString());
			     flag = 1;
			 }
		 }
		 if(flag == 0)
			 System.out.println("no such teacher");
	 }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...