1.要求:能够输入名字显示其信息,输入年级也能该年级所有人的信息,输入所属项目也能显示该项目组的所有成员的信息。
2.思路:用二维数组录入各成员信息,因为数组类型必须一致,所以全部选择string类型,剩下的就是匹配输入与数组内容,遍历数组,输出信息。
3.代码:
import java.util.Scanner;
class LabSearch
{
public static void main(String[] args)
{
boolean answer = true;
Scanner scan = new Scanner(System.in);
while(answer){
String[][] labperson = {
//这个二维数组里面写各成员信息
};
//Scanner scan = new Scanner(System.in);
System.out.print("请选择要查询的方式(姓名查询请填1/年级查询请填2/项目查询请填3):");
int way = scan.nextInt();
switch(way){
case 1:
System.out.print("请输入要查询的名字:");
String name = scan.next();
System.out.println("姓名" + "\t" + "年龄" + "\t" + "年级" + "\t" + "专业" + "\t\t" + "所属项目");
int i,j;
for(i = 0;i < labperson.length;i++){
for(j = 0;j<labperson[i].length;j++){
if(labperson[i][j].compareTo(name) == 0){
for(j = 0;j<labperson[i].length;j++){
System.out.print(labperson[i][j] + "\t");
}
System.out.println();
}
}
}
System.out.print("请选择是否重新查询(y/n):");
String huida1 = scan.next();
if(huida1.equals("y")){
answer = true;
}else if(huida1.equals("n")){
answer = false;
}else{
System.out.print("输入有误,程序结束!");
answer = false;
}
break;
case 2:
System.out.print("请输入要查询的年级(1/2/3):");
String nianji = scan.next();
for(i = 0;i < labperson.length;i++){
for(j = 0;j<labperson[i].length;j++){
if(labperson[i][j].compareTo(nianji) == 0){
for(j = 0;j<labperson[i].length;j++){
System.out.print(labperson[i][j] + "\t");
}
System.out.println();
}
}
}
System.out.print("请选择是否重新查询(y/n):");
String huida2 = scan.next();
if(huida2.equals("y")){
answer = true;
}else if(huida2.equals("n")){
answer = false;
}else{
System.out.print("输入有误,程序结束!");
answer = false;
}
break;
case 3:
System.out.print("请输入要查询的项目:");
String project = scan.next();
for(i = 0;i < labperson.length;i++){
for(j = 0;j<labperson[i].length;j++){
if(labperson[i][j].compareTo(project) == 0){
for(j = 0;j<labperson[i].length;j++){
System.out.print(labperson[i][j] + "\t");
}
System.out.println();
}
}
}
System.out.print("请选择是否重新查询(y/n):");
String huida3 = scan.next();
if(huida3.equals("y")){
answer = true;
}else if(huida3.equals("n")){
answer = false;
}else{
System.out.print("输入有误,程序结束!");
answer = false;
}
break;
default:
System.out.println("你输入的内容有误!");
}
}
scan.close();
}
}