как я могу получить возврат из статического массива? - PullRequest
1 голос
/ 08 мая 2019

Понятия не имею, как бороться со статическим массивом.Когда я запускаю код, он говорит: «не может быть преобразован в 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 голосов
/ 08 мая 2019

Ваш вопрос слишком двусмысленный, но, возможно, это поможет вам:

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;
}
...