试图通过用户输入填充2D数组怎么做? [英] trying to fill a 2D array by user-input how to do it?

查看:64
本文介绍了试图通过用户输入填充2D数组怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个允许用户输入其输入以创建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

解决方案

 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; 

}

这将为您提供所需的输出,并在 return 语句之前将其添加到 fill 方法中

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

3 4

5 6

解决方案

 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 fill method before the return statement

这篇关于试图通过用户输入填充2D数组怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆