本文主要是介绍Student类信息查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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};// 通过name、email、address查询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() {}/*** 带所有参数的构造器* @param name 姓名* @param age 年龄* @param gender 性别* @param phone 电话* @param address 地址* @param email 邮箱*/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("睡觉的方法");}@Overridepublic 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查询:
这篇关于Student类信息查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!