在Java中,如何接受用户的值并将其存储到2D数组中?
我已经创建了一个二维数组来接受用户的值。但是,当我运行代码并尝试输入值时,它不接受这些值在Java中,如何接受用户的值并将其存储到2D数组中?,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我已经创建了一个二维数组来接受用户的值。但是,当我运行代码并尝试输入值时,它不接受这些值 int rows = 0; int cols = 0; int[][] Array = new int[rows][cols]; Scanner entry = new Scanner(System.in); for(int i = 0; i < rows; i++){ for(int j = 0; j < cols; j+
int rows = 0;
int cols = 0;
int[][] Array = new int[rows][cols];
Scanner entry = new Scanner(System.in);
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
Array[rows][cols] = entry.nextInt();
}
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.println(Array[rows][cols]);
}
}
int行=0;
int cols=0;
int[][]数组=新的int[行][cols];
扫描仪条目=新扫描仪(System.in);
对于(int i=0;i
行和列的值为0,您正在创建一个具有行和列大小的数组。因此,数组的大小为0,不接受任何值。尝试更改行和列的值
int rows = 5;
int cols = 5;
int[][] Array = new int[rows][cols]; // new int[5][5];
Scanner entry = new Scanner(System.in);
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
Array[i][j] = entry.nextInt();
}
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
System.out.println(Array[i][j]);
}
}
int行=5;
int cols=5;
int[][]数组=新的int[行][cols];//新国际[5][5];
扫描仪条目=新扫描仪(System.in);
对于(int i=0;i
Array[i][j]
-不是Array[rows][cols]
任何一种方式,rows=cols=0
所以你的数组有空间容纳0
元素。我可以通过用Array[i][j]重放数组[rows][cols]来解决这个问题。谢谢你,我感谢你的帮助//例如int cols=5;int[][]数组=新的int[行][cols]//int[]数组=新的int[5][5];现在您有了一个数组//大小为5 Scanner entry=new Scanner(System.in);for(int i=0;i