class YangHui
{
public static void main(String[] args)
{
int n=10;
int yh[][]=new int[10][19];
yh[(0)][yh[9].length-10]=1;
for(int i=1;i<10;i++)
{
for(int j=1;j<18;j++)
{
yh[i][j]=yh[i-1][j-1]+yh[i-1][j+1];

if(yh[i][j]==0)
{
System.out.print(" ");
}
else
{
System.out.print(yh[i][j]+" ");
}
}
System.out.println("");
}
}
}
打印出来的图形是这样的
E:\JavaProject\Project>java YangHu
       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
 9  36  84  126  126  84  36  9
 高手应该懂的我想打印什么了 
第一行少了一个1
最后一行的第一个元素喝醉后一个元素也少了1
不知到是哪里错了请教高手打印出完整的杨辉三角 
我还是新手不要用太高深的东西
就把我上面的代码改一改就行了 
谢谢了

解决方案 »

  1.   

    杨辉三角。。
    public static void main(String args[]) {
    int arr[][] = new int[7][7]; int i, j;
    for (i = 1; i < 7; i++) {
    for (j = 1; j <= i; j++) {
    if ((i == 0) || (i == j)) {
    arr[i][j] = 1;
    System.out.print(arr[i][j] + " ");
    if (i == 6)
    break;
    System.out.println("");
    } else {
    arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
    System.out.print(arr[i][j] + " ");
    } } }
    }
      

  2.   


    public static void main(String[] args) { int yh[][] = new int[11][21];
    for (int i = 1; i < 11; i++) {
    for (int j = 1; j < 20; j++) {
    if (i == 1 && j == 10) {
    yh[i][j] = 1;
    } else {
    yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j + 1];
    }
    if (yh[i][j] == 0) {
    System.out.print(" ");
    } else {
    System.out.print(yh[i][j]);
    }
    }
    System.out.println();
    }
    }
      

  3.   

    我要的不是这种的直角三角形 是等腰三角形class YangHui
    {
    public static void main(String[] args)
    {
    int n=10;
    int yh[][]=new int[10][19];
    yh[(0)][9]=1;
    yh[9][0]=1;
    yh[9][18]=1;
    for(int i=1;i<10;i++)
    {
    for(int j=1;j<18;j++)
    {
    yh[i][j]=yh[i-1][j-1]+yh[i-1][j+1];


    }
    System.out.println();
    }
    for(int x=0;x<yh.length;x++)
    {
    for(int y=0;y<19;y++)
    {
    if(yh[x][y]==0)
    {
    System.out.print(" "+"  ");
    }
    else
    {
    System.out.print(yh[x][y]+"  ");
    }

    }
    System.out.println();
    }

    }
    }
    帮忙给一下这个也行 
    定义一个功能函数
      

  4.   

    public static void yanHu() {
    int[][] a = new int[10][10];
    for (int i = 0; i <= 9; i++) {
    a[i][0] = 1;
    a[i][i] = 1;
    }
    for (int i = 2; i <= 9; i++)
    for (int j = 1; j <= i - 1; j++)
    a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
    for (int i = 0; i <= 9; i++) {
    for (int j = 0; j <= i; j++) {
    if (a[i][j] > 9 && a[i][j] < 100)
    System.out.print("  " + a[i][j]);
    if (a[i][j] > 99)
    System.out.print(" " + a[i][j]);
    if(a[i][j] > 0 && a[i][j] <10)
    System.out.print("   " + a[i][j]);
    }
    System.out.println("");
    }
    }
      

  5.   

    public class YangHui {
    public static void main(String[] args) {
    int n = 10;
    int yh[][] = new int[n][2*n+2];
    yh[0][n] = 1;
    for (int i = 0; i < n; i++){
    for (int j = 0; j <2*n+1; j++){
    if(i != 0&&j !=0 ){
    yh[i][j] = yh[i-1][j-1] + yh[i-1][j+1];
    }
    StringBuffer sb =new StringBuffer();
    sb.append("            ");
    if (yh[i][j]==0){

    sb.setLength(4);
    System.out.print(sb.toString());
    } else {
    String str =String.valueOf(yh[i][j]);
    sb.setLength(4-str.length());
    System.out.print(str+ sb.toString());
    }
    }
    System.out.println("");
    }
    }
    }
      

  6.   

    其他的方法都是用于输出格式的方法。Java code                             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    10    45   120   210   252   210   120    45    10     1     1    11    55   165   330   462   462   330   165    55    11     1  1    12    66   220   495   792   924   792   495   220    66    12     1
    这个就很规范啊 。