像下面程式码打印出来的样子
0和9没有真的对齐
(PTT上看起来有对齐,但Eclipse上没有)
-9 -9 0 0
0 0 0 -9
0 -9 0 -9
-9 -9 0 0
printf要怎么改才能漂亮对齐
就仿佛0的前面有看不见的加号那样
thank
程式码:
class Data {
public void wall() {
int[][] grid = new int[][] {
{ -9, 0, 0, -9 },
{ -9, 0, -9, -9 },
{ 0, 0, 0, 0 }, { 0, -9, -9, 0 },
{ -9, 0, -9, 0 },
{ -9, 0, 0, 0 }, { 0, 0, -9, 0 } };
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 4; i++) {
System.out.printf("%2d" + " ",
grid[i][j]);
}
System.out.print("\n");
}
}
}
public class Wall {
public static void main(String[] args) {
// TODO Auto-generated method stub
Data data = new Data();
data.wall();
}
}