CSDN博客

img faintbear

java数字金字塔(转)

发表于2004/11/2 12:53:00  2076人阅读

分类: java文章

1>
     class ThreeDay {
 public static void display(int loop) {

  int width = loop * 2 - 1;

  for (int i = 1; i <= loop; i++) {
   for (int j = 1; j <= (width - (2 * i - 1)) / 2 ; j++) {
    System.out.print(" ");
   }
   for (int j = 1; j <= i; j++) {
    System.out.print(j);
   }
   for (int j = i - 1; j > 0; j--) {
    System.out.print(j);
   }   
   System.out.println("");   
  }

 }
 public static void main(String[] args) {
  display(10);
 }
}


2>

  public class testsan {

  public static void pyramid(int curRow,int maxRow) {
    if (curRow == maxRow ) return;
    int count = 0;
    int num = 0;
    do {
      if (count == 0)
        for (int i = 0; i < maxRow - curRow; i++)
          System.out.print(" ");

      if (count <= curRow)
         System.out.print(++num);
      else
           System.out.print(--num);
      count ++;
    } while (count < 2*curRow+1);
    System.out.println();
    pyramid(++curRow,maxRow);
  }

  public static void main(String[] args) {
     pyramid(0,5);
  }

}


3>

  public class testfei {

  public void pyramid(int curRow,int maxRow) {
    int count = 0;
    int num = 0;
    do {
      if (count == 0)
        for (int i = 0; i < maxRow - curRow; i++)
          System.out.print(" ");

      if (count <= curRow)
         System.out.print(++num);
      else
           System.out.print(--num);
      count ++;
    } while (count < 2*curRow+1);
    System.out.println();
  }


  public static void main(String[] args) {
      testfei aa = new testfei();
      for (int i = 0; i < 5;i ++)
         aa.pyramid(i,5);
  }

}



4>


   public class shuzita {

 public static void main(String[] args) {
  int row=10;
  for(int i=1;i<=row;i++){
   System.out.print(getSpace(row-i));
   System.out.print(getLeftNumber(i));
   System.out.print(getRightNumber(i));
   System.out.println();
  }
 }
 private static String getSpace(int number){
  StringBuffer buffer=new StringBuffer();
  for(int i=1;i<=number;i++){
   buffer.append(" ");
  }
  return buffer.toString();
 }
 private static String getLeftNumber(int number){
  StringBuffer buffer=new StringBuffer();
  for(int i=1;i<=number;i++){
   buffer.append(i);
  }
  return buffer.toString();
 }
 private static String getRightNumber(int number){
   StringBuffer buffer=new StringBuffer();
   for(int i=number-1;i>=1;i--){
    buffer.append(i);
   }
   return buffer.toString();
  }
}

阅读全文
0 0

相关文章推荐

img
取 消
img