2016-09-24 62 views
4

我正在嘗試創建交替「*」和「o」字符的三角形金字塔,其行數基於用戶輸入。預期結果我想實現的,如果用戶輸入「6」的行數,方法是:Java - 如何創建交替的三角形金字塔?

 * 
    *o* 
    *o*o* 
    *o*o*o* 
    *o*o*o*o* 
*o*o*o*o*o* 

我寫來實現此功能的代碼是:

String star = "*"; 
String circle = "o"; 
System.out.println("Please enter number of rows: "); 
int rows = in.nextInt(); 
for (int i = 0; i < rows; i++){ 
    for (int j = 0; j < rows-i; j++){ 
     System.out.print(star); 
    } 
    for (int k = 0; k <= i; k++){ 
     System.out.print(circle); 
    } 
    System.out.println(); 
} 

然而,我的代碼的輸出與上面的金字塔不匹配。我的代碼輸出,與「6」的用戶輸入,方法是:

******o 
*****oo 
****ooo 
***oooo 
**ooooo 
*oooooo 

花前3個小時煮兩本網站和其他人,我還是來了丟失如何交替的字符後,如何在每一行中使用正確數量的字符,以及如何將金字塔格式化爲預期的輸出。我不知道如果我的代碼是完全錯誤的,或者如果我只是錯過了一部分,使其正常工作,但任何意見或引用非常感謝。

+0

您是否已經檢查了[爲循環創建三角形](http://stackoverflow.com/questions/11409621/creating-a-triangle-with-for-loops)? –

回答

2

這種方法將正常工作:

public void printPyramid (int input) { 
    for (int row = 1; row <= input; row++) { 
     for (int whitespace = input - 1; whitespace >= row; whitespace--) { 
      System.out.print(" "); 
     } 
     System.out.print("*"); 
     for (int circle = 1; circle < row; circle++) { 
      System.out.print("o*"); 
     } 
     System.out.println(); 
    } 
} 
      * 
         *o* 
         *o*o* 
         *o*o*o* 
        *o*o*o*o* 
        *o*o*o*o*o* 
        *o*o*o*o*o*o* 
        *o*o*o*o*o*o*o* 
       *o*o*o*o*o*o*o*o* 
       *o*o*o*o*o*o*o*o*o* 
       *o*o*o*o*o*o*o*o*o*o* 
       *o*o*o*o*o*o*o*o*o*o*o* 
      *o*o*o*o*o*o*o*o*o*o*o*o* 
      *o*o*o*o*o*o*o*o*o*o*o*o*o* 
      *o*o*o*o*o*o*o*o*o*o*o*o*o*o* 
      *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o* 
     *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o* 
     *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o* 
     *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o* 
     *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o* 
    *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o* 
+0

謝謝大家的回覆!他們絕對有幫助,給了我一些新的想法!馬塞爾的回答最終成爲我自己最容易遵循的答案,而且效果很好! – ccbadger

5

你可以用另一種更簡單的方法來處理它。

在僞代碼:

  • 創建的n空格的字符串
  • 添加"*"
  • 循環n次,每次循環:
    • 打印它
    • " *"替換爲"*O*"

這就承認一個簡單的方法來創建第一線,和一個簡單的方法來創建與上一行的下一行。每個替換將僅匹配最後(前導)空間和第一顆恆星,用星號替換空間,用O替換星號並添加星號。

通常解決困難問題的最好方法是以一種簡單的問題(或一組簡單問題)來看待問題。


一對夫婦的方式來創建的n空格的字符串:

  • 一個循環,增加' '每次迭代
  • new String(new char[n]).replace('\0', ' ')

如何替換字符串的某些字符與其他字符:

str = str.replace(" *", "*O*"); 
1

歡迎來到Stack Overflow! 首先,「o」和「*」不交替,因爲for循環會一直執行直到完成。這意味着星星和圈子將分別打印出來。對於這個應用程序,您只需要一個for循環和兩個if語句,根據for循環中的「i」是奇數還是偶數。一個簡單的方法就是用模函數:

String star = "*"; 
String circle = "o"; 
System.out.println("Please enter number of rows: "); 
int rows = in.nextInt(); 
for (int i = 0; i < rows; i++){ 
    if ((i % 2) == 0) 
    { 
     System.out.print(circle); 
    } 
    else 
    { 
     system.out.print(star); 
    } 
System.out.println(); 
} 

看看是否有效。 謝謝!

1

這是一個解決方案,易於理解和初學者友好。
(如果你想要去更高級的,看的解決方案,從@波希米亞♦)

String star = "*"; 
String circle = "o"; 
System.out.println("Please enter number of rows: "); 
int rows = in.nextInt(); 
for (int i = 0; i < rows; i++){ 
    // How many "o" do we need? 
    int count_circle = i; 

    // How many * do we need? 
    int count_star = count_circle + 1; 

    // Let's create the string with o and * 
    String output = ""; 
    for(int j = 0; j < (count_circle + count_star); j++){ 
     if(j % 2 == 0) // if it is odd 
      output = output + star; 
     else // if it is even 
      output = output + circle; 
    } 

    // Do we need spaces at the beginning? 
    String spaces = ""; 
    for(int j = 0; j < rows - i - 1; j++){ 
     spaces = spaces + " "; 
    } 

    // Final output 
    output = spaces + output; 
    System.out.println(output); 
} 
1

試試這個。

Scanner in = new Scanner(System.in); 
System.out.println("Please enter number of rows: "); 
int rows = in.nextInt(); 
for (int i = 0; i < rows; ++i) { 
    System.out.printf("%" + (rows - i) + "s", "*"); 
    for (int j = 0; j < i; ++j) 
     System.out.print("o*"); 
    System.out.println(); 
}