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

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();

    }

尝试在表/索引中显示数组,但当我这样做时,它只是垂直显示随机数。Idk如何修复它。抱歉,新手代码..:(


慕丝7291255
浏览 78回答 2
2回答

德玛西亚99

在java中,java中的二维数组只是数组的数组,并且在迭代它时会出现一个小错误。在外部 for 循环中添加System.out.println();for(int x = 0; x< 22; x++) {&nbsp; &nbsp; &nbsp;// for every array in outer array&nbsp; &nbsp; &nbsp;for(int y = 0; y < 9; y++)&nbsp; {&nbsp; &nbsp;//for every double in each inner array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers[x][y] = (int)(Math.random()*192)+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(numbers[x][y]+ "&nbsp; ");&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; }&nbsp;System.out.println();&nbsp;}

千巷猫影

如果将构造和显示分开,则可能会更清晰:double numbers[][] = new double[22][9];// constructionfor(int x = 0; x<22; x++)&nbsp; &nbsp; for(int y = 0; y <9; y++)&nbsp; &nbsp; &nbsp; &nbsp; numbers[x][y] = (int)(Math.random()*192)+1;// displayfor(int x = 0; x<22; x++){&nbsp; &nbsp; for(int y = 0; y <9; y++)&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(numbers[x][y]+ "\t");&nbsp; &nbsp; System.out.println("");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java