java二维数组杨辉三角多位数对齐

杨辉三角前面的还好说,但是到后面出现多位数,就对不起了,
import java.util.*;
public class yangHui {
public static void main(String args[]) {
int n;
Scanner input = new Scanner(System.in);
System.out.print("请输入行数n:");
n = input.nextInt();
int mat[][] = new int[n][];
int i,j;
for (i = 0; i < n; i++) {
mat[i] = new int[i + 1];
mat[i][0] = 1;
mat[i][i] = 1;
for (j = 1; j < i; j++){
mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
}
}
for (i = 0; i < mat.length; i++){
for (j = 1; j < n - i; j++)
System.out.print(" ");
for (j = 0; j < mat[i].length; j++)
System.out.print(" " + mat[i][j]);
System.out.println("");
}
}
}
这是我自己写的,再怎么改啊,要求所有行对齐,求高手指教,附详细解释

可以考虑数组,也就是在把内容均衡放到数组里面

或者教你个馊主意,在打印输出的时候把字符串格式化,强制格式化为譬如5长度宽追问

能不能详细点,或者给出程序,我是新手,有些弄不懂

追答

import
java.util.Scanner;

public
class yangHui {

publicstaticvoid main(String args[]) {

final String SPACE = " ";

int n;

Scanner input =
new Scanner(System.in);

System.
out.print("please input row number: ");

n = input.nextInt();

int mat[][] = newint[n][n];

int i, j;

for (i = 0; i < n; i++) {

mat[i] =
newint[i + 1];

mat[i][0] = 1;
mat[i][i] = 1;

for (j = 1; j < i; j++) {

mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
}
}

for (i = 0; i < mat.length; i++) {

for (j = 1; j < n - i; j++)

System.
out.print(SPACE);

for (j = 0; j < mat[i].length; j++)

System.
out.print(" " + format(mat[i][j], SPACE));

System.
out.println(SPACE);

}
}

privatestatic String format(int i, String str) {

StringBuilder sb =
new StringBuilder();

sb.append(i);

while(sb.length() < str.length()){

sb.insert(0,
" ");

}

return sb.toString();

}
}

追问

please input row number: 10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

还是不行啊,我想要的是所有行中心对称的那种,就是1、2、6、20、70直线上的

追答

哪个不是你现在的代码能完成的

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-03
package com.gem.test.small;
class Q{
public void array(int i) {
int array[][]=new int[i][i];
for (int j = 0; j < array.length; j++) {
for (int x = 0; x < i-j; x++) {
System.out.print("\t");
}
for (int j1 = 0; j1 <=j; j1++) {
if (j1==0) {
System.out.print("\t"+(array[j][j1]=1)+"\t");
}else{
if (j1==j) {
System.out.print("\t"+(array[j][j1]=1)+"\t");
}else{
System.out.print("\t"+(array[j][j1]=array[j-1][j1-1]+array[j-1][j1])+"\t");
}
}
}System.out.println();
}
}
}
public class Triangle {
public static void main(String[] args) {
Q q=new Q();
q.array(10);

}
}
第2个回答  2012-08-02
把空格改为制表符,因为制表符占4个字符的长度,基本能满足你的需求。
System.out.print("\t" + mat[i][j])追问

这个知道,但是这么写间隔太大,看着不美观 。有没有更好的办法啊?

追答

那你想想,如果你输出的最大位数有5位,这个时候你必然要让数字占五位才能达到你要的全部对齐。问题是,那些一位数的数字也必须要占5位,所以间隔大是必要的。

所谓的补位,就是说输出的最大数是5位时,将不满五位的数字全部补满五位。但肯定不是用0补,应该用空格。这个不难,问题是,这个补位的话仅在最大数位数为2到3位或超过4位时有优势,而且补位需要对每一个数做判断(当然,最大数可以通过最后一行来获取),从而确定补几位,最后还是会有大间隔。

补位方法:
public static String fillSpace(int num){
String strNum = String.valueOf(num);
int len = strNum.length;
for(int i=0;i<len;i++){
strNum = " " + strNum;
}
return strNum;
}

然后在你输出的时候就要这样输出:
System.out.print(" " + fillSpace(mat[i][j]));

第3个回答  2012-08-02
按最大数补位, 如1变成01 或001等追问

我知道补位,但是不知道具体怎么写,,你能写具体吗?

追答

把楼上的空格换成0, 空格多难看! 参差不齐的