java问题,求解答

如题所述

按照你的要求编写的Java程序如下:

public class D {

 public static void main(String[] args) {

  int levelcount =5;

  int [][]yanghui= new int[levelcount][0];

  for(int i=0;i < levelcount;i++){

   yanghui[i]=new int[i+1];

   for(int j=0;j<=i;j++){

    if(i==0){

     yanghui[i][j]=1;

    }else{

     int value=0;

     if(j>=1){

      value+=yanghui[i-1][j-1];

     }

     if(j<i){

      value+=yanghui[i-1][j];

     }

     yanghui[i][j]=value;

    }

   }

  }

  for(int i=0;i<levelcount;i++){

   for(int j=0;j<levelcount-i;j++){

    System.out.print (" ");

   }

   for(int j=0;j<=i;j++){

    if(j==i)

     System.out.print(yanghui[i][j]);

    else

     System.out.print(yanghui[i][j]+"-");

   }

   System.out.println();

  }

 }

}

运行结果:

     1
    1-1
   1-2-1
  1-3-3-1
 1-4-6-4-1

温馨提示:内容为网友见解,仅供参考
第1个回答  2016-04-13
public class test{
public static void main(String[]args){
int a[][]=new int[5][5];
for (int i=0;i<a.length ;i++ )
{
for (int j=0;j<a[i].length ;j++ )
{
if (j==0||i==j)
{
a[i][j]=1;
}
else{
if (i>j)
{
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
}
}
for (int i=0;i<a.length ;i++ )
{
for (int j=0;j<a[i].length ;j++ )
{
if (i>=j)
{
System.out.print(a[i][j]+"-");
}
}
System.out.println();
}
}
}
第2个回答  2016-04-13
你这个是没有规律的,不能循环输出,至少我不会
public class Demo {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int k = 5 - i; k > 0; k--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(i);
System.out.print(" ");
}
System.out.println();
}
}
}
最终输出:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
第3个回答  2016-04-13
1-5-10-10-5-1对吗追问

我是要Java代码啊

相似回答