从另一个方法调用数组

时间:2018-08-06 15:47:06

标签: java

public static double[] displayGrades() {

    Scanner input = new Scanner(System.in);

    int students =12;
    int questions = 10;
    double correct =1.5;
    double wrong = 0.5;

    int[][]answers = {{3,4,2,5,0,2,1,3,2,4},{0,0,2,1,5,4,1,2,3,1},{3,3,2,5,4,1,2,5,0,3},{3,4,4,5,3,5,4,0,3,1},{3,4,2,5,4,3,1,2,3,2},{1,3,4,3,2,5,4,2,1,0},{2,0,3,4,2,0,1,5,4,2},{3,4,5,3,3,2,4,1,2,5},{3,5,3,5,1,2,3,4,5,6,0},{3,4,5,3,4,3,0,0,0,0},{3,4,5,2,1,2,3,2,5,3},{3,2,1,5,5,3,2,5,2,4}};
    int[] correctA = {3,4,2,5,4,3,1,2,3,1};

    double score [] = new double [students];

    for (int i =0; i<students ; i++) {
        for (int j=0; j<questions; j++) {
            if (answers[i][j] ==correctA[j])
                score[i] += correct;
            else if (answers[i][j] != 0)
                score[i] -= wrong;
        }

    }

    return score;
}

public static void main(String[] args)  {

    displayGrades(score);
    int[]id= {1,1,1,2,2,2,3,3,3,4,4,4};

    System.out.printf("%s \t%s", "Student Group", "Score" );
    for(int i=0; i<students; i++) 
        System.out.printf("\n   %d \t\t%.2f", id[i],  score[i] );
}

我是Java的新手,我无法解决这一问题。 Main方法无法识别score数组和students变量。我无法打印出来。根据作业,我必须使用两种方法来完成。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

displayGrades()是不接受任何参数的方法,但是您正在尝试将scores传递给它。另外,您还没有定义scores,我想您想做的是:

double[] scores = displayGrades();

这会将displayGrades()的结果解析为double数组scores

您也可以代替for(int i=0; i<students; i++),直到scores.length为止(假设students == scores.length。即,分数与学生一样多)