【问题标题】:how can I get return from static array?我怎样才能从静态数组中获得回报?
【发布时间】:2019-09-25 22:10:26
【问题描述】:

我不知道如何处理静态数组。 当我运行代码时,它显示“无法转换为 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 ;
    }
}

【问题讨论】:

  • 您需要创建一个学生数组并用学生对象填充它。

标签: arrays methods static return


【解决方案1】:

你的问题太模棱两可了,但也许这会对你有所帮助:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2011-04-11
    • 1970-01-01
    • 2020-10-07
    相关资源
    最近更新 更多