java-创建学生数组并根据id,name,age,gender搜索学生信息

创建StudentManager类

public class StudentManger {

	
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		System.out.println("请输入学生的数量:");
		int count=scanner.nextInt();
		Student[] students=new Student[count];
		//遍历该数组
		for (int i = 0; i < students.length; i++) {
			System.out.println("请输入学生的id: ");
			int id=scanner.nextInt();
			System.out.println("请输入学生的name: ");
			scanner.nextLine();
			String name=scanner.nextLine();
			System.out.println("请输入学生的gender: ");
			scanner.nextLine();
			String gender=scanner.nextLine();
			System.out.println("请输入学生的age: ");
			int age=scanner.nextInt();
			Student student=new Student(id, name, age, gender);
			students[i]=student;
		}
		while (true) {
			System.out.println("------------------------");
			System.out.println("根据id来搜索:1");
			System.out.println("根据name来搜索:2");
			System.out.println("根据age来搜索:3");
			System.out.println("根据gender来搜索:4");
			System.out.println("退出:0");
			System.out.println("------------------------");
			System.out.println("请输入要进行的操作编号:");
			int type=scanner.nextInt();
			if (type==0) {
				System.out.println("谢谢使用!");
				break;
			}
			
			switch (type) {
			case 1:
				System.out.println("请输入要搜索的id编号:");
				int id=scanner.nextInt();
				boolean findId=false; 
				for (int i = 0; i < students.length; i++) {
					if (id==students[i].getId()) {
						System.out.println(students[i]);
						findId=true;
					}
				}
				if (!findId) {
					System.out.println("没有找到!");
				}
				break;
			case 2:
				System.out.println("请输入要搜索的姓名:");
				String name=scanner.nextLine();
				boolean findName=false;
				for (int i = 0; i < students.length; i++) {
					if (name.equals(students [i].getName())) {
						System.out.println(students[i]);
						findName=true;
					}
				}
				if (!findName) {
					System.out.println("没有找到!");
				}
				break;
			case 3:
				System.out.println("请输入要搜索的年龄:");
				int age=scanner.nextInt();
				boolean findAge=false;
				for (int i = 0; i < students.length; i++) {
					if (age==students[i].getAge()) {
						System.out.println(students[i]);
						findAge=true;
					}
				}
				if (!findAge) {
					System.out.println("没有找到!");
				}
				break;
			case 4:
				System.out.println("请输入要搜索的性别:");
				String gender=scanner.nextLine();
				boolean findGender=false;
				for (int i = 0; i < students.length; i++) {
					if (gender.equals(students [i].getGender())) {
						System.out.println(students[i]);
						findGender=true;
					}
				}
				if (!findGender) {
					System.out.println("没有找到!");
				}
				break;
			default:
				System.out.println("输入错误,请重新输输入!");
				break;
			}
		}
	}
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值