【问题标题】:trying to fill a 2D array by user-input how to do it?试图通过用户输入填充二维数组怎么做?
【发布时间】: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


【解决方案1】:
 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; 

}

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

【讨论】:

  • 是的,我改变了我的代码,看看我更新的问题,现在我有另一个问题。它只是输入数字而不显示我显示最后一个输出的二维数组
  • 你希望你的输出怎么样?
  • 我将再次编辑我的问题以显示它的输出外观
  • 这个答案将构建一个二维数组,但不使用用户输入我想要的是让用户输入数字来构建它
【解决方案2】:

把你的 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];

【讨论】:

  • 是的,现在我明白了问题所在,我更改了代码,但现在系统显示另一个错误:**在线程“main”java.lang.ArrayIndexOutOfBoundsException 中输入 MatrixException 的元素:exam3Programing1_20_12_2010 中的 0。考试 3Programing1_20_12_2010.Question2.main(Question2.java:45) 上的 Question2.fill(Question2.java:33) **
  • 为什么系统显示ArrayIndexOutOfBoundsException当我从0开始并按array.length-1进行与时??
  • 为什么要创建一个长度为 0 的新数组 new int[0][0]; ??
  • 哦,这是个错误...但知道它只是输入数字而没有为数组创建任何内容..我将更新我的问题并在我更改代码后显示最后的输出看看就在上面
【解决方案3】:

试试这个:

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;

【讨论】:

  • 我试过你的解决方案,但它给了我一个错误**在线程“main”java.lang.ArrayIndexOutOfBoundsException:0 atexam3Programing1_20_12_2010.Question2.fill(Question2.java: 31) 在考试 3Programing1_20_12_2010.Question2.main(Question2.java:43) **
  • @Darshan Lila 这个答案将构建一个二维数组,但不使用用户输入我想要的是让用户输入数字来构建它
  • 你想要用户指定大小的数组吗?
猜你喜欢
  • 2019-04-14
  • 1970-01-01
  • 2021-02-10
  • 2018-04-16
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2020-10-07
  • 2019-04-06
相关资源
最近更新 更多