试图通过用户输入填充2D数组怎么做?

时间:2014-12-26 08:05:44

标签: java for-loop user-input multidimensional-array

我正在创建一个代码,允许用户输入他的输入来创建一个2D数组,但它不起作用,如果有人能帮助我,我不知道问题出在哪里我会很感激。

这是我的代码:

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

3 个答案:

答案 0 :(得分:2)

 public int[][] fill(){
      int[][] data = new int[row][col];  
      Scanner in = new Scanner(System.in) 

     .....

      return data; 

  } 

您要将数据数组声明为长度

 [0][0]

这就是错误的原因。将语句更改为上面给出的代码。

<强>更新

   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; 

}

这将为您提供所需的输出,在 返回 声明之前将其添加到 填充 方法

答案 1 :(得分:0)

试试这个:

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;

答案 2 :(得分:0)

将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;
}

<强>原因:

您当前的fill()方法未将值保存到数组中 你必须加上这一行

 a[row][col]=input.nextInt();

使用

  int x = in.nextInt();
           system.out.print(x);

您只是在int变量x

上反复输入数据

<强>更新

更改此

 int[][] data = new int[0][0];

到这个

 int[][] data = new int[row][col];