我正在尝试编写一个需要使用三种方法的代码。第一种方法必须使用嵌套的 while 循环来创建 3x3 的多维数组,并将从命令行获取值。第二种方法需要嵌套循环的顶级使用,以便计算每行中双精度的平均值。最后一种方法需要显示这些平均值的结果。我目前收到无法最终符号错误。我做错了什么?
我创建了方法fillArray来存储来自用户的值。返回值是在命令行输入的所有值。
import java.util.Scanner;公共类记分器 {
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;
}
}
该代码应允许为每列输入 3 个值,然后能够计算每行值的平均值并显示这些结果。
while(i < scores[i].length){
在第三次交互中,你将得到一个 indexofboundsexception。I = 3 -> 分数[3].长度
System.out.println(scores);
未在主方法中声明分数。
System.out.println(scores);
我从未测试过这个,但您正在尝试打印矢量。 不知道这是否有效