【发布时间】:2015-02-23 14:38:32
【问题描述】:
我正在创建一个代码,允许用户输入他的输入来创建一个二维数组,但它不起作用,因为如果有人可以帮助我,我不知道问题出在哪里,我将不胜感激。
这是我的代码:
package test7;
import java.util.Scanner;
public class test7 {
private int row = 4;
private int col = 4;
private int[][] matrix;
public test7(int trow, int tcol) {
this.row = trow;
this.col = tcol;
}
public test7(int trow, int tcol, int[][] m) {
this.row = trow;
this.col = tcol;
this.matrix = m;
}
public int[][] fill(){
int[][] data = new int[row][col];
Scanner in = new Scanner(System.in);
for(int row = 0; row< matrix.length; row++){
for(int col = 0 ;col< matrix[row].length; col++){
System.out.println("enter the elementss for the Matrix");
data[row][col] = in.nextInt();
}
System.out.println();
}
return data;
}
public static void main(String[] args){
int[][] ma = new int[3][2];
test7 q2 = new test7(3, 2,ma);
q2.fill();
}
}
输出:
enter the elementss for the Matrix
4
enter the elementss for the Matrix
3
enter the elementss for the Matrix
5
enter the elementss for the Matrix
8
enter the elementss for the Matrix
9
enter the elementss for the Matrix
0
输出应该是这样的:
1 2
3 4
5 6
【问题讨论】:
-
您的
fill方法没有填充。 -
怎么不填??我以为我正在使用 for 循环获取用户输入并添加到二维数组中......这不是吗?
-
您认为具体哪一行正在向数组添加值?
-
现在我更改了 for 循环中的代码,但现在它显示和错误
-
@java dev,具体点,显示什么错误,你到底是怎么改代码的?
标签: java for-loop user-input multidimensional-array