在处理用于制作不规则2D阵列的代码时,我发现了一个奇怪的错误,同时弄乱了列的不同输入值。当行工作时,列长度输入返回错误的数量或发生空指针错误。我不确定是什么导致这个,因为(1,2,3)等输入返回正确的表但(2,1,3)不会。同样在4行中,列输入(2,3,4,5)返回“索引超出范围异常:5”,因为应该保留它的while循环不应该超出范围在合理的范围内。主要和显示方法似乎都没有正确地保存预期的列长度,我似乎无法找到原因。 似乎数组设置为3行,列长度为1,2,3。 第(3)行和第(2,3,1)列的输出给出:
A:2.0
B:2.0 2.0
C:2.0 2.0 2.0
我想要的是:
A:2.0 2.0
B:2.0 2.0 2.0
C:2.0
代码:
import java.util.Scanner;
//================================================================
public class ArrayIrreg {
//----------------------------------------------------------------
private static Scanner Keyboard = new Scanner(System.in);
public static void main(String[] args) {
//----------------------------------------------------------------
char group, rLetter,letter;
String choice;
int sum = 0;
int num = 10; // for test
int rows = 10;
int columns = 8;
//greetings
System.out.println("");
System.out.println("Welcome to the Band of the Hour");
System.out.println("-------------------------------");
// creating 2d array
System.out.print("Please enter number of rows : ");
rows = Keyboard.nextInt();
Keyboard.nextLine();
while (rows < 0 || rows >= 10) {
System.out.print("ERROR:Out of range, try again : ");
rows = Keyboard.nextInt();
Keyboard.nextLine();
}
double[][] figures = new double[rows + 1][num];
for(int t = 0; t < rows; t++) {
rLetter = (char)((t)+(int)'A');
System.out.print("Please enter number of positions in row " + rLetter + " : ");
columns = Keyboard.nextInt();
Keyboard.nextLine();
while(columns < 0 || columns >= 8) {
System.out.print("ERROR:Out of range, try again : ");
columns = Keyboard.nextInt();
Keyboard.nextLine();
}
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
}
// filling the array
for(int row = 0; row < figures.length; ++row) {
for(int col = 0; col < figures[row].length; ++col) {
figures[row][col] = 2.0;
}
}
// printing the array
for(int row = 1; row < figures.length; ++row) {
// printing data row
group = (char)((row-1)+(int)'A');
System.out.print(group + " : ");
for(int col = 0; col < figures[row].length; ++col) {
System.out.print(figures[row][col] + " ");
System.out.print(" ");
}
System.out.print("["+","+avg(figures)+"]");
System.out.println();
}
//----------MENU
System.out.print("(A)dd, (R)emove, (P)rint, e(X)it : ");
choice = Keyboard.next();
letter = choice.charAt(0);
letter = Character.toUpperCase(letter);
if(letter == 'P') {
display(figures);
}
}
public static void display(double x[][]) {
int average, total;
char group;
System.out.println(" ");
for(int row=1;row<x.length;row++) {
group = (char)((row-1)+(int)'A');
System.out.print(group+" : ");
for(int column=0;column<x[row].length;column++){
System.out.print(x[row][column]+" ");
}
System.out.print("["+","+avg(x)+"]");
System.out.println();
}
}
public static int avg(double[][] temp) {
int sum = 0;
int avg = 0;
for (int col = 0; col < temp[0].length; col++) {
sum = 0;
for (int row = 0; row < temp.length; row++)
sum += temp[row][col];
System.out.println(sum);
}
avg = sum / temp.length;
return avg;
}
}
答案 0 :(得分:0)
就是这个
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
我认为你的意思是
figures[t] = new double[columns];
答案 1 :(得分:0)
在创建2D阵列时,
更改此
double[][] figures = new double[rows + 1][num];
到
double[][] figures = new double[rows][num];
和
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
到
figures[t] = new double[columns] ;
在打印数组中
更改此
for(int row = 1; row < figures.length; ++row) {
group = (char)((row-1)+(int)'A');
到
for(int row = 0; row < figures.length; ++row) {
group = (char)((row)+(int)'A');
显示功能
更改此
for(int row = 1; row < x.length; ++row) {
group = (char)((row-1)+(int)'A');
到
for(int row = 0; row < x.length; ++row) {
group = (char)((row)+(int)'A');