Java 试图通过用户输入填充二维数组怎么做?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27654491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
trying to fill a 2D array by user-input how to do it?
提问by java dev
i am creating a code that allow user to enter his input to create a 2D array but it did not work as it should i do not know where is the problem if anyone can help me i will appreciate that.
我正在创建一个代码,允许用户输入他的输入来创建一个二维数组,但它不起作用,因为它应该我不知道问题出在哪里,如果有人可以帮助我,我将不胜感激。
this my code :
这是我的代码:
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();
}
}
the output:
输出:
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
the output should look exactly like this:
输出应如下所示:
1 2
1 2
3 4
3 4
5 6
5 6
采纳答案by varun
public int[][] fill(){
int[][] data = new int[row][col];
Scanner in = new Scanner(System.in)
.....
return data;
}
You were declaring data array to length
您正在将数据数组声明为长度
[0][0]
This is why the error is. Change the statement to above given code.
这就是错误的原因。将语句更改为上面给定的代码。
UPDATE
更新
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();
}
for(int row = 0; row< matrix.length; row++){
for(int col = 0 ;col< matrix[row].length; col++){
System.out.println(data[row][col]);
}
System.out.println();
}
return data;
}
}
This will give you the desired output , add it to fillmethod before the returnstatement
这将为您提供所需的输出,将其添加到return语句之前的fill方法中
回答by Darshan Lila
Try this:
尝试这个:
System.out.println("enter the elementss for the Matrix");
Scanner in = new Scanner(System.in);
int[][] data = new int[matrix.length][];//note the change here
for(int i = 0; i< matrix.length; i++){
data[i]=new int[matrix[i].length]; // note the the change here
for(int j = 0 ;j< matrix[i].length; j++){
data[i][j] = in.nextInt(); // note the change here
}
system.out.println();
}
return data;
回答by Syeda Zunaira
Replace your fill() method to this
将您的 fill() 方法替换为此
public int[][] fill(){
int a[][]=new int[row][col];
Scanner input = new Scanner(System.in);
System.out.println("enter the elementss for the Matrix");
for(int row=0;row<3;row++){
for(int col=0;col<3;col++){
a[row][col]=input.nextInt();
}
}
return a;
}
Reason:
原因:
Your current fill()
method is not saving values to the array
you must ad this line
您当前的fill()
方法未将值保存到您必须添加此行的数组中
a[row][col]=input.nextInt();
using
使用
int x = in.nextInt();
system.out.print(x);
you are just entering the data again and again on the int
variable x
您只是一次又一次地在int
变量上输入数据x
Update
更新
change this
改变这个
int[][] data = new int[0][0];
to this
对此
int[][] data = new int[row][col];