Java修复我的多维数组填充



尝试在java中获得多维数组填充如下:通过传递高度和宽度为10 &10,乘数为10

预期输出:

(0, 10)(0, 20)(30 0,)(0, 40)(0, 50)(0, 60)(0, 70)(0, 80)(0, 90)

(10,10)(10、20)(10、30)(10, 40)(10、50)60 (10)(70)(80)(90)

(20、10)(20、20)(20、30)等等…

代码:

    int[][] coords = new int[height][width];
    int multiplier = 10;
    for (int i = 0;i < height;i++) {
        for (int j = 0;j < width;j++){
            coords[i][j] = j*multiplier; 
        }
    }
    for (int i = 0;i < height-1;i++) {
        for (int j = 0;j < width;j++){
            System.out.println("(" + i + "," + coords[i][j] + ")"); 
        }
    }
当前输出:

(0, 0)(0, 10)(0, 20)(30 0,)(0, 40)(0, 50)(0, 60)(0, 70)(0, 80)(0, 90)

(1,0)(10)(20)(30)(40)(50)(60)(70)(80)(90)

(2,0)(10)(20)(30)(40)(50)(60)(70)(80)(90)

(0)(10)(20)(30)(40)(50)(60)(70)(80)(90)

(4 0)(4、10)(20)(4, 30)(4, 40)(4、50)(4、60)(70)(80)(90)

(5,0)(5, 10)(5、20)(5、30)(40)(50)(5、60)(70)(80)(90)

(0)(10)(20)(30)(40)(50)(60)(70)(80)(90)

(7,0)(7, 10)(7日20)(7、30)(7, 40)50 (7)60 (7)(70)(80)(90)

(8,0)(8、10)20(8日)30(8日)40(8日)50(8日)60(8日)(70)(80)(90)

更改以匹配所需的输出:

  • j初始值变为1
  • 仅在外循环中打印新行
  • 打印
  • 时将i乘以10

_

int[][] coords = new int[height][width];
int multiplier = 10;
for (int i = 0;i < height;i++) {
    for (int j = 1;j < width;j++){
        coords[i][j] = j*multiplier;
    }
}
for (int i = 0;i < height-1;i++) {
    for (int j = 1;j < width;j++){
        System.out.print("(" + i*multiplier + "," + coords[i][j] + ")");
    }
    System.out.println();
}

相关内容

  • 没有找到相关文章