简体   繁体   English

为什么我的 main 中的打印输出语句不显示在我的 fillArray 方法中输入的值?

[英]Why isn't the print out statement in my main displaying the values that were entered in my fillArray method?

I'm trying to write a code where I need to use three methods.我正在尝试编写需要使用三种方法的代码。 The first method must use a nested while loop to create a multidimensional array that is 3x3 and will get the values from the command line.第一种方法必须使用嵌套的 while 循环来创建一个 3x3 的多维数组,并将从命令行获取值。 The second method needs top use nested for loops in order to calculate the average of the doubles in each row.第二种方法需要 top 使用嵌套的 for 循环,以便计算每行双精度值的平均值。 And the final method needs to shows the result of those averages.最后一个方法需要显示这些平均值的结果。 I'm currently getting cannot final symbol error.我目前遇到无法最终符号错误。 What did I do wrong?我做错了什么?

I created the method fillArray to have the values from the user be stored.我创建了方法 fillArray 来存储来自用户的值。 The return value is all the values entered at the command line.返回值是在命令行输入的所有值。

import java.util.Scanner;导入 java.util.Scanner; public class Scorer {公共课记分员{

public static void main(String[] args) {
    fillArray();
    System.out.println(scores);
}

public static double [][] fillArray(){
    double [][] scores = new double [3][3];
    Scanner scnr = new Scanner(System.in);
    int col = 3;
    int row = 3;

    System.out.println("Enter your scores: ");

    int i = 0;
    while(i < scores[i].length){
        int j = 0;
        while(j < scores[i].length){
            scores[i][j] = scnr.nextDouble();
            j++;
        }
        i++;
        }
    return scores;
    }

}

The code should allow for 3 values to be entered for each column and then be able to calculate the average of each rows values and have those results displayed back.该代码应允许为每列输入 3 个值,然后能够计算每行值的平均值并显示这些结果。

while(i < scores[i].length){

in the third interaction you will get a indexofboundsexception.在第三次交互中,您将获得一个 indexofboundsexception。 I = 3 -> scores[3].length I = 3 -> 分数[3].length

System.out.println(scores);

score was not declared in the main method. score 未在 main 方法中声明。

System.out.println(scores);

I've never tested this, but you're trying to print a vector.我从来没有测试过这个,但你正在尝试打印一个矢量。 don't know if this work不知道这是否有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM