简体   繁体   English

JAVA使用用户输入填充2D数组

[英]JAVA populate 2D array with user input

I am trying to populate an NxN matrix. 我正在尝试填充NxN矩阵。 What I'd like to do is be able to enter all the elements of a given row as one input. 我想要做的是能够输入给定行的所有元素作为一个输入。 So, for example if I have a 4x4 matrix, for each row, I'd like to enter the 4 columns in one input, then print the matrix after each input showing the new values. 因此,例如,如果我有一个4x4矩阵,对于每一行,我想在一个输入中输入4列,然后在显示新值的每个输入后打印矩阵。 I try to run the following code but I get an error that reads: Exception in thread "main" java.util.InputMismatchException. 我尝试运行以下代码,但我得到一个错误:线程“main”中的异常java.util.InputMismatchException。 Here is my code: 这是我的代码:

     double twoDm[][]= new double[4][4];
     int i,j = 0;
     Scanner scan = new Scanner(System.in).useDelimiter(",*");

     for(i =0;i<4;i++){
         for(j=0;j<4;j++){
             System.out.print("Enter 4 numbers seperated by comma: ");
             twoDm[i][j] = scan.nextDouble();

         }
     }

When I get the prompt to enter the 4 numbers I enter the following: 当我收到输入4个数字的提示时,我输入以下内容:

1,2,3,4

Then I get the error. 然后我得到了错误。

You should just do this; 你应该这样做;

double twoDm[][] = new double[4][4];
Scanner scan = new Scanner(System.in);
int i, j;

for (i = 0; i < 4; i++) {
  System.out.print("Enter 4 numbers seperated by comma: ");
  String[] line = scan.nextLine().split(",");
  for (j = 0; j < 4; j++) {
    twoDm[i][j] = Double.parseDouble(line[j]);

  }
}

scan.close();

You should not forget to close the scanner too! 你不应该忘记关闭扫描仪!

1 2 3 4 are not visible by Scanner as double numbers, but as integers. 扫描仪不能看到1 2 3 4为双数字,而是整数。

So you have the following possibilities: 所以你有以下几种可能性:

  • If you don't need a double use nextInt() 如果你不需要double使用nextInt()
  • Write 1.0,2.0,3.0,4.0 instead of 1,2,3,4 写1.0,2.0,3.0,4.0而不是1,2,3,4
  • Read the values as strings and convert them to double with Double.parseDouble() 将值读取为字符串并使用Double.parseDouble()将它们转换为double

I believe it would be easier to use string.split() , rather than .useDelimiter() , because when using delimiter , you would have to input the last number with a comma as well (since comma is the one delimiting stuffs) unless you create some regex to take both comma and \\n as delimiter. 我相信使用string.split()而不是.useDelimiter()更容易,因为在使用分隔符时,你必须用逗号输入最后一个数字(因为comma是一个分隔的东西),除非你创建一些正则表达式,将逗号和\\n作为分隔符。

Also, you should give the prompt - System.out.print("Enter 4 numbers separated by comma: "); 此外,您应该提示 - System.out.print("Enter 4 numbers separated by comma: "); inside the outer loop, not the inner loop, since you would be taking in each row inside the outer loop, and only elements in each row in the inner loop. 在外部循环内部,而不是内部循环,因为您将接收外部循环内的每一行,并且只接受内部循环中每行中的元素。

You can do - 你可以做 -

double twoDm[][]= new double[4][4];
int i,j = 0;
Scanner scan = new Scanner(System.in);
for(i =0;i<4;i++){
   System.out.print("Enter 4 numbers separated by comma: ");
   String row = scan.nextLine().split(",");
   for(j=0;j<4;j++){
      twoDm[i][j] = Double.parseDouble(row[j]);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM