2013-12-11 50 views
0

輸入:鹼= 2,行= 3
輸出:我想用Java創建一個「*」的權力金字塔。

** 
    **** 
******** 

輸入:鹼= 3,ROW = 3
輸出:

  *** 
     ********* 
*************************** 

我曾嘗試這種方式,但我空間打印不正確。

import java.util.Scanner; 

public class loops { 
    public static void main(String[] args) { 
     Scanner s=new Scanner(System.in); 
     System.out.println("enter base:"); 
     int base = s.nextInt(); 

     System.out.println("enter height:"); 
     int h = s.nextInt(); 


     for (int i = 1; i <= h; i++) { 

      int num = (int)Math.pow(base, i); 
       for(int n=h-1; n>i-1; n--) { 
         System.out.print(" "); 

        } 

       for (int j = 0; j < num; j++) { 
        System.out.print("*"); 
       } 

      System.out.println(""); 
     } 
    } 
} 
+0

這似乎有點眼熟...... – Kyle

+0

@Kyle - 同樣的老+一些代碼...... –

+0

啊,這解釋了它:P – Kyle

回答

1
import java.util.Scanner; 

public class loops { 
    public static void main(String[] args) { 
     Scanner s=new Scanner(System.in); 
     System.out.println("enter base:"); 
     int base = s.nextInt(); 

     System.out.println("enter height:"); 
     int h = s.nextInt(); 

     int spacesNum; 
     int asterisksNum; 

     for (int i = 1; i <= h; i++) { 

      spacesNum = (int) ((Math.pow(base, h) - Math.pow(base, i))/2); 
      asterisksNum = (int) (Math.pow(base, i)); 

      for (int j = 0; j < spacesNum; j++) { 
       System.out.print(" "); 
      } 

      for (int j = 0; j < asterisksNum; j++) { 
       System.out.print("*"); 
      } 

      System.out.println(); 

     } 

     s.close(); 
    } 
} 
+0

這工作,thax –

0

的空間寬度幾何級數增長,就像環的寬度,但在相反的方向 - 它衰減。編寫代碼最簡單的方法可能是考慮總寬度,以及每個環的內容。

你與考慮代碼:

public class loops { 
    public static void main(String[] args) { 
     Scanner s = new Scanner(System.in); 
     System.out.println("enter base:"); 
     int base = s.nextInt(); 

     System.out.println("enter height:"); 
     int h = s.nextInt(); 
     int width = (int) Math.pow(base, h); // Note the total width. 

     for (int i = 1; i <= h; i++) { 
      int num = (int) Math.pow(base, i); 

      // The space is half of what's left after removing the ring. 
      for(int j = 0; j < (width - num)/2; j++) { 
       System.out.print(" "); 
      } 

      for (int j = 0; j < num; j++) { 
       System.out.print("*"); 
      } 

      System.out.println(""); 
     } 
    } 
} 
0

您的代碼電話System.out.print方法指數倍,每個字符的一個電話。

另外,在每次迭代中調用System.out.println,這會導致基礎流刷新。 (請參閱評論中的michaelt的鏈接)。

這個SO answer是一個很好的參考。

這是不好的做法,因爲:進行

  1. h的I/O操作,這是昂貴的。
  2. print和println的很多方法調用降低了代碼的可讀性。

撰寫串在一個單獨的方法,並使用僅System.out.print用於打印。

請參考下面的代碼:

public static void main(String[] args) { 
     //your code here 

    int totalWidth = (int) Math.pow(base, h); 
    String output = ""; 

    for (int i = 1; i <= h; i++) { 
     int numOfStars = (int) Math.pow(base, i); 
     int numOfSpace = (int) ((totalWidth - numOfStars)/2); 
     output += composeString(' ', numOfSpace).concat(composeString('*', numOfStars).concat("\n")); 
    } 
    System.out.println(output); 
    } 


    //Method to create String with same character repeated X number of times 
    public static String composeString(char character, int x) { 
     StringBuilder buf = new StringBuilder(x); 
     while (buf.length() < x) { 
      buf.append(character); 
     } 
     return buf.toString(); 
    } 
+1

如果你關心性能......建設者而不是緩衝區。使用StringBuilder(int)構造函數而不是「」。追加而不是插入。 – 2013-12-11 06:10:44

+1

特別 - [追加]之間的差(http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/AbstractStringBuilder.java#AbstractStringBuilder.append %28char%29)和[插入](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/AbstractStringBuilder.java#AbstractStringBuilder.insert% 28int%2Cchar%29)是'insert'調用中的System.arrayCopy,它非常昂貴。 – 2013-12-11 06:16:01

+0

@MichaelT:編輯我的答案,非常感謝:-) –