有带参方法实现查找一个学生类数组里的一个学生姓名

时间:2023-01-24 10:55:33
package Month.JUL.JUL09;

//学员类
public class Student {
    String name;
    public Student() {}
    public Student(String name) {
        this.name = name;
    }
}


package Month.JUL.JUL09;

import java.util.Scanner;

public class Test02 {
    static Scanner input = new Scanner(System.in);

    public void chaZhao(int start, int end, String stu, Student[] stus) {

        for (int i = (start - 1); i < end; i++) {
            if (stus[i].name.equals(stu)) {
                System.out.println("找到了,在第" + (i + 1) + "个位置!");
                return;
            }
        }
    }

    public static void main(String[] args) {
        Student[] stus = new Student[10];
        for (int i = 0; i < 10; i++) {
            stus[i] = new Student("张" + (i + 1));
        }

        System.out.println("请输入开始查找的位置(1~10之间):");
        int start = input.nextInt();
        System.out.println("请输入结束查找的位置(1~10之间):");
        int end = input.nextInt();
        System.out.println("请输入要查找的人的姓名:");
        String st = input.next();
        Student stu = new Student(st);
        Test02 ca = new Test02();
        ca.chaZhao(start, end, st, stus);

    }

}

有带参方法实现查找一个学生类数组里的一个学生姓名