java—填充对象数组的问题结果总是空的

ie3xauqp  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(320)

我创建了一个三节课的课程。在第三个类中,我应该用“name”、“姓氏”、“code”和“age”值填充数组。但是,我的数组总是“null”,我无法填充它。谢谢大家的帮助。
头等舱:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String name, surname;
    int code, age, i=0, min=150;        

    System.out.println("How many teachers do you want to enter information?");
    i=in.nextInt();     

    for(int j=0; j<i; j++) 
    {
        System.out.println("Enter the teacher's name");
        name=in.next();
        System.out.println("Enter the teacher's surname");
        surname=in.next();
        System.out.println("Enter the teacher's code");
        code=in.nextInt();
        System.out.println("Enter the teacher's age");
        age=in.nextInt();

        Teacher d = new Teacher(name, surname, code, age);
        System.out.println(d.getCode());
        System.out.println(d.getSurname());
        System.out.println(d.getAge());

        University c = new University(name, surname, code, age);
        min=c.MinimumAge(age, min);

    }

    System.out.println(min);

}
}

二等舱:

public class Teacher
{
    protected String name;
    protected String surname;
    protected int code;
    protected int age;

    public Teacher(String name, String surname, int code, int age)
    {
        this.name=name;
        this.surname=surname;
        this.code=code;
        this.age=age;
    }

    public String getName()
    {
        return name;
    }

    public int getCode()
    {
        return code;
    }

    public String getSurname()
    {
        return surname;
    }

    public int getAge()
    {
        return age;
    }
}

第三类(我将“0”作为填充数组的位置,以便进行测试):

public class University
{
    private Teacher[] array=new Teacher[4];;
    public University(String name, String surname, int code, int age)
    {
        array[0] = new Teacher(name, surname, code, age);
    }

    public int MinimumAge(int age, int min)
    {
        if (age<min)
            min=age;
        return min;
    }
}
vohkndzv

vohkndzv1#

这个 class University 未正确实施。您可以将其实现为:

class University {
    private int counter = 0;
    private final int MAX = 4;
    private Teacher[] array = new Teacher[MAX];

    public void addTeacher(Teacher teacher) {
        if (counter < MAX) {
            array[counter++] = teacher;
        }
    }

    public int minimumAge() {
        if (counter == 0) {
            return 0;
        }
        int min = array[0].getAge();
        for (int i = 1; i < counter; i++) {
            if (array[i].getAge() < min) {
                min = array[i].getAge();
            }
        }
        return min;
    }
}

因此,您需要在中更改代码 main 如下图所示:

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String name, surname;
        int code, age, i = 0, min = 150;

        University university = new University();

        System.out.print("How many teachers do you want to enter information?: ");
        i = in.nextInt();

        for (int j = 0; j < i; j++) {
            System.out.print("Enter the teacher's name: ");
            name = in.next();
            System.out.print("Enter the teacher's surname: ");
            surname = in.next();
            System.out.print("Enter the teacher's code: ");
            code = in.nextInt();
            System.out.print("Enter the teacher's age: ");
            age = in.nextInt();

            university.addTeacher(new Teacher(name, surname, code, age));
        }

        System.out.println(university.minimumAge());
    }
}

示例运行:

How many teachers do you want to enter information?: 2
Enter the teacher's name: a
Enter the teacher's surname: b
Enter the teacher's code: 1
Enter the teacher's age: 23
Enter the teacher's name: x
Enter the teacher's surname: y
Enter the teacher's code: 2
Enter the teacher's age: 15
15

相关问题