java Student类信息查询
代码
import java.util.Scanner;
public class Student {
private String name;
private int age;
private String gender;
private String phone;
private String address;
private String email;
public static void main(String[] args) {
Student student1 = new Student("张三",18,"男","111111","广州","abc@qq.com");
Student student2 = new Student("李四",20,"男","222222","四川","efg@qq.com");
Student student3 = new Student("王五",33,"女","333333","西安","hij@qq.com");
Student[] students = new Student[]{
student1,
student2,
student3
};
Scanner scanner =new Scanner(System.in);
while (true){
boolean flag =false;
System.out.println("请输入name或email或address查询:");
String nextLine = scanner.nextLine();
for (Student student : students) {
if (student.name.equals(nextLine) || student.email.equals(nextLine) || student.address.equals(nextLine)) {
System.out.println("找到了" + student.name + "的学生信息:" + student);
flag = true;
break;
}
}
if (!flag){
System.out.println("没有查询到任何信息!请重新输入查询条件");
}
}
}
public Student() {
}
public Student(String name, int age, String gender, String phone, String address, String email) {
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.address = address;
this.email = email;
}
public void eat(){
System.out.println("吃的方法");
}
public void drink(){
System.out.println("喝的方法");
}
public void play(){
System.out.println("玩的方法");
}
public void sleep(){
System.out.println("睡觉的方法");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", email='" + email + '\'' +
'}';
}
}
输出
请输入name或email或address查询:
12
没有查询到任何信息!请重新输入查询条件
请输入name或email或address查询:
李四
找到了李四的学生信息:Student{name='李四', age=20, gender='男', phone='222222', address='四川', email='efg@qq.com'}
请输入name或email或address查询:
王五
找到了王五的学生信息:Student{name='王五', age=33, gender='女', phone='333333', address='西安', email='hij@qq.com'}
请输入name或email或address查询:
张三
找到了张三的学生信息:Student{name='张三', age=18, gender='男', phone='111111', address='广州', email='abc@qq.com'}
请输入name或email或address查询:
李
没有查询到任何信息!请重新输入查询条件
请输入name或email或address查询:
鸡蛋
没有查询到任何信息!请重新输入查询条件
请输入name或email或address查询:
第
没有查询到任何信息!请重新输入查询条件
请输入name或email或address查询: