简体   繁体   English

创建2D数组并用随机数填充数组

[英]Creating a 2D Array and filling the array with random numbers

double numbers[][];
numbers = new double[22][9];

for(int x = 0; x<22; x++) {
    for(int y = 0; y <9; y++)
    {
        numbers[x][y] = (int)(Math.random()*192)+1;
        System.out.print(numbers[x][y]+ "");
        System.out.println();
    }

Trying to display the array within a table/index but when I do, it just display the random numbers vertically. 试图在表/索引中显示数组,但是当我这样做时,它只是垂直显示随机数。 Idk how to fix it. Idk如何修复它。 Sorry for the nooby code.. :( 对不起,nooby代码.. :(

In java two-dimensional array in java is just an array of array, and a small mistake while iterating it. 在Java中,二维数组在Java中只是一个数组数组,并且在迭代时存在一个小错误。 Add System.out.println(); 添加System.out.println(); in outer for loop 在外部for循环中

for(int x = 0; x< 22; x++) {     // for every array in outer array
     for(int y = 0; y < 9; y++)  {   //for every double in each inner array

          numbers[x][y] = (int)(Math.random()*192)+1;
          System.out.print(numbers[x][y]+ "  ");     
      }
 System.out.println(); 

}

If you separate construction and display, it may be clearer: 如果将构造和显示分开,则可能会更清楚:

double numbers[][] = new double[22][9];

// construction
for(int x = 0; x<22; x++)
    for(int y = 0; y <9; y++)
        numbers[x][y] = (int)(Math.random()*192)+1;

// display
for(int x = 0; x<22; x++){
    for(int y = 0; y <9; y++)
        System.out.print(numbers[x][y]+ "\t");
    System.out.println("");
}

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

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