修改教师类,使得由多个Teacher对象所形成的数组可以排序(编号由低到高排序),并在main函数中使用Arrays.sort(Object[]a)方法排序,定义一个类TeacherManagement,包含教师数组,提供方法add(Teacher[]),使其可以添加教师,提供重载方法search,方法可以在一组给定的教师中,根据姓名或年龄返回等于指定姓名或年龄的教师的字符串信息,信息格式为:“no:, name:, age: **, seminary: **”。如果没有满足条件的教师,则返回“no such teacher”。
教师个数 教师信息 待查找教师的姓名 待查找教师的年龄
排序后的信息 按姓名查找的老师信息 按年龄查找的老师信息
在这里给出一组输入。例如:
4
3 Linda 38 SoftwareEngineering
1 Mindy 27 ComputerScience
4 Cindy 28 SoftwareEngineering
2 Melody 27 ComputerScience
Cindy
27
在这里给出相应的输出。例如:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
no: 3, name: Linda, age: 38, seminary: SoftwareEngineering
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by name:
no: 4, name: Cindy, age: 28, seminary: SoftwareEngineering
search by age:
no: 1, name: Mindy, age: 27, seminary: ComputerScience
no: 2, name: Melody, age: 27, seminary: ComputerScience
java弱弱的我现在只能用笨方法了
package jyc.test;
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String [] args){
Scanner input =new Scanner(System.in);
int n=input.nextInt();
Teacher [] obj=new Teacher[n];
int i=0;
while (i<n){
obj[i]=new Teacher();
obj[i].no=input.nextInt();
obj[i].name=input.next();
obj[i].age=input.nextInt();
obj[i].dept=input.next();
i++;
}
String sname=input.next();
int sage=input.nextInt();
int t;//排序
String ss;
for(int i0=0;i0<n;i0++){
for(int j0=i0;j0<n-1;j0++){
if(obj[j0].no>obj[j0+1].no){
t=obj[j0].no;obj[j0].no=obj[j0+1].no;obj[j0+1].no=t;
ss=obj[j0].name;obj[j0].name=obj[j0+1].name;obj[j0+1].name=ss;
t=obj[j0].age;obj[j0].age=obj[j0+1].age;obj[j0+1].age=t;
ss=obj[j0].dept;obj[j0].dept=obj[j0+1].dept;obj[j0+1].dept=ss;
}
}
}
//Arrays.sort(obj);
for(int j=0;j<i;j++){
System.out.println(obj[j].toString());
}
int fg=0;
System.out.println("search by name:");
for( i=0;i<n;i++){
if(obj[i].name.equals(sname)){
fg=1;
System.out.println(obj[i].toString());
}
}
if(fg==0)
System.out.println("no such teacher");
fg=0;
System.out.println("search by age:");
for( i=0;i<n;i++){
if(obj[i].age==sage){
fg=1;
System.out.println(obj[i].toString());
}
}
if(fg==0)
System.out.println("no such teacher");
}
}
class Teacher {
int no;
String name;
int age;
String dept;
public String toString() {
String s = "no: " + this.no + ", name: " + this.name + "," +
" age: " + this.age + ", seminary: " + this.dept;
return s;
}
}
注:本文原创,转载请注明原处