楼主:
dharma (é”)
2015-05-27 02:26:34像下面程式码打印出来的样子
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();
}
}
作者: ctrlbreak 2015-05-27 08:17:00
作法应该是先找出最大的数值所占的空间(长度)后,再依每个数值长短分别补上该有的空间.想要漂亮不是一个printf可以办到的