主类
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static void main(String[] args) { 7 // 通过Scanner可以从控制台读取用户输入的数据 8 Scanner scanner = new Scanner(System.in); 9 ArrayList<Student> list = new ArrayList<Student>(); 10 11 while (true) { 12 // Unreachable code 13 System.out.println("请输入id:"); 14 int id = scanner.nextInt(); 15 System.out.println("请输入名字:"); 16 String name = scanner.next(); 17 System.out.println("请输入年龄:"); 18 int age = scanner.nextInt(); 19 System.out.println("请输入性别:"); 20 String gender = scanner.next(); 21 Student student = new Student(id, name, age, gender); 22 list.add(student); 23 24 System.out.println("您是否要退出: -1退出, 0继续"); 25 int quit = scanner.nextInt(); 26 if (quit == -1) { 27 break; 28 } 29 } 30 31 for (Student student : list) { 32 System.out.println(student); 33 } 34 35 while (true) { 36 System.out.println("-------------------------"); 37 System.out.println("根据id搜索输入1"); 38 System.out.println("根据name搜索输入2"); 39 System.out.println("根据age搜索输入3"); 40 System.out.println("根据gender搜索输入4"); 41 System.out.println("您是否要退出: -1退出, 0继续"); 42 System.out.println("-------------------------"); 43 int type = scanner.nextInt(); 44 if (type == -1) { 45 System.out.println("退出程序"); 46 break; 47 } 48 boolean search = false;//未找到该学生 49 switch (type) { 50 case 1:// 根据id搜索 51 System.out.println("请输入要搜索的id"); 52 int id = scanner.nextInt(); 53 for (Student student : list) { 54 if (id == student.getId()) { 55 System.out.println(student); 56 search = true; 57 } 58 } 59 if(search == false) { 60 System.out.println("没有该id的学生"); 61 } 62 break; 63 case 2:// 根据name搜索 64 System.out.println("请输入要搜索的名字:"); 65 String name = scanner.next(); 66 for (Student student : list) { 67 // 在java里面判断字符串的内容是不是相等使用equal的方法 68 if (name.equals(student.getName())) { 69 System.out.println(student); 70 search = true; 71 } 72 } 73 if(search == false) { 74 System.out.println("没有该名字的学生"); 75 } 76 break; 77 case 3:// 根据age搜索 78 System.out.println("请输入要搜索的年龄"); 79 int age = scanner.nextInt(); 80 for (Student student : list) { 81 if (age == student.getAge()) { 82 System.out.println(student); 83 search = true; 84 } 85 } 86 if(search == false) { 87 System.out.println("没有该年龄的学生"); 88 } 89 break; 90 case 4:// 根据gender搜索 91 System.out.println("请输入要搜索的性别:"); 92 String gender = scanner.next(); 93 for (Student student : list) { 94 // 在java里面判断字符串的内容是不是相等使用equal的方法 95 if (gender.equals(student.getGender())) { 96 System.out.println(student); 97 search = true; 98 } 99 } 100 if(search == false) { 101 System.out.println("没有该性别的学生"); 102 } 103 break; 104 105 default: 106 break; 107 } 108 } 109 110 } 111 }
类
1 public class Student { 2 private int id; 3 private String name; 4 public int age; 5 private String gender; 6 7 public int getId() { 8 return id; 9 } 10 11 public void setId(int id) { 12 this.id = id; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public Student(int id, String name, int age, String gender) { 20 super(); 21 this.id = id; 22 this.name = name; 23 this.age = age; 24 this.gender = gender; 25 } 26 27 public void setName(String name) { 28 this.name = name; 29 } 30 31 public Student() { 32 super(); 33 // TODO Auto-generated constructor stub 34 } 35 36 public int getAge() { 37 return age; 38 } 39 40 public void setAge(int age) { 41 this.age = age; 42 } 43 44 public String getGender() { 45 return gender; 46 } 47 48 public void setGender(String gender) { 49 this.gender = gender; 50 } 51 52 @Override 53 public String toString() { 54 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + "]"; 55 } 56 }