我如何从静态数组中获得收益?

时间:2019-05-08 08:12:29

标签: arrays methods static return

我不知道如何处理静态数组。 当我运行代码时,它说“无法转换为Student []”。 什么是正确的回报类型?

public class Student{

String name;
int physic;
int chemistry;
int math;

    public Student(String name, int physic, int chemistry, int math) {
    this.name = name;
    this.physic = physic;
    this.chemistry = chemistry;
    this.math = math;
    }
}

class Main {

    private static Student[] readData(String filename) {
        String[] name = {"John", "Alice", "Johnson", "Dennis", "Jack", "Tod", "Tom", "Dave", "David", "Trent", "Bob", "Fiona", "Peter", "Amy", "Nancy", "Richard", "Daniel", "James", "Cathy", "Paul"};
        int[] physic = {95, 88, 95, 60, 77, 84, 68, 90, 99, 89, 100, 77, 80, 85, 83, 81, 77, 80, 95, 84};
        int[] chemistry = {75, 95, 75, 100, 84, 86, 70, 90, 70, 77, 67, 89, 88, 95, 93, 91, 78, 90, 74, 87};
        int[] math = {88, 75, 88, 100, 93, 80, 75, 92, 87, 90, 89, 90, 82, 78, 82, 86, 79, 85, 89, 79};

        return ;
    }
}

1 个答案:

答案 0 :(得分:0)

您的疑问太含糊,但这也许可以帮助您:

private static Student[] readData(String filename) {
    String[] name = {"John", "Alice", "Johnson", "Dennis", "Jack", "Tod", "Tom", "Dave", "David", "Trent", "Bob", "Fiona", "Peter", "Amy", "Nancy", "Richard", "Daniel", "James", "Cathy", "Paul"};
    int[] physic = {95, 88, 95, 60, 77, 84, 68, 90, 99, 89, 100, 77, 80, 85, 83, 81, 77, 80, 95, 84};
    int[] chemistry = {75, 95, 75, 100, 84, 86, 70, 90, 70, 77, 67, 89, 88, 95, 93, 91, 78, 90, 74, 87};
    int[] math = {88, 75, 88, 100, 93, 80, 75, 92, 87, 90, 89, 90, 82, 78, 82, 86, 79, 85, 89, 79};

    Student[] students = new Student[name.length];
    for(int i=0; i<name.length; i++){
        students[i] = new Student(name[i], physic[i], chemistry[i], math[i]);
    }
    return students;
}