2016-05-31 123 views
0

好吧我很累這個掙扎着,蝙蝠我感覺真的很愚蠢,所以請溫柔..我已經搜索了堆棧溢出和網絡,仍然沒有找到任何東西。我正在使用嵌套循環創建一個如下所示的三角形:java三角形左側的嵌套循環邏輯

   1 
      1 2 1 
     1 2 4 2 1 
    1 2 4 8 4 2 1 

...等等。通過128中心列。

我的左側循環呈現在我的三角形的右側,而不是左側。我很確定,在我收到答覆後,我會用手掌敲我的頭,然後反覆地說。無論如何感謝您的幫助。我特別想要解釋一下邏輯。這是代碼。

public class Pyramid_center_x2_0519 { 

public static void main(String[] args) { 

    for (int centerColumn = 1; centerColumn <= 128; centerColumn *=2){ 
     for (int j = 8; j > 1; j--) { 
      System.out.printf("%7s", ""); 
     } 

     for (int rightSide = centerColumn; rightSide > 0; rightSide/=2){ 

      System.out.printf("%7d", rightSide); 
     } 

     for (int leftSide = 2; leftSide < centerColumn; leftSide*=2){ 

      System.out.print(leftSide);   
     } 

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

這裏是它呈現的方式:再次 enter image description here

謝謝.......

更新的代碼下午12點5-31-16:我理解Java打印從左到右。下面我把左邊的循環放在右邊的循環之前,但是我在三角形前面的間距不起作用。謝謝大家......再次

for (int centerColumn = 1; centerColumn <= 128; centerColumn *=2){ 
     for (int j = 8; j > 0; j--) { 

      System.out.printf("%7s", ""); 
     } 
     for (int leftSide = 1; leftSide < centerColumn; leftSide*=2){ 

      System.out.print(leftSide);   
     } 
     for (int rightSide = centerColumn; rightSide > 0; rightSide/=2){ 

      System.out.printf("%7d", rightSide); 
     } 

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

}

+0

你正在嘗試簡單地做不起作用。您無法打印「向左」。 printf只是繼續寫作的方向寫作。 –

+0

您必須更改您的algorythm並實際按您希望顯示的順序編寫數字。這個邏輯不應該那麼難。 –

回答

0

那麼,你已經解決了你的大部分問題,你最近更新的版本已經正確地打印了數字。

你唯一需要解決的問題是格式化,以使它看起來不錯。

從當前的代碼開始有2件事情需要改變:(7)

  1. 你所有的印刷數字應該佔用相同的空間。您已經以這種方式格式化了您的「右側」打印號碼,您只需對其他號碼進行相同的操作即可。

  2. 您目前總是在行前添加8 * 7個空格,當然這是不正確的。如果你看金字塔,你可以清楚地看到第一行有8 * 7個空格是正確的,但是第二行將需要7 * 7個空格在前面,第三個6 * 7等等。這意味着要得到正確的格式爲你金字塔,你必須修改你的循環,打印空間運行你的主循環的每次迭代少1次。

這裏有一種方法,你可以如何更新您的代碼中添加這些2個變化(我添加了註釋的行前我改變的解釋):

public static void main (String[] args) throws java.lang.Exception 
{ 
    /** I added this counter to keep track 
    * of what iteration/line the loop currently is (see 2.) 
    */ 
    int iteration = 0; 
    for (int centerColumn = 1; centerColumn <= 128; centerColumn *=2){ 

     /** Here we changed the loop condition 
     * from "j > 0" to "j > iteration" 
     * (So as iteration gets bigger, the loop runs less often) 
     */ 
     for (int j = 8; j > iteration; j--) { 
      System.out.printf("%7s", ""); 
     } 
     for (int leftSide = 1; leftSide < centerColumn; leftSide*=2){ 
      /** This should be self explanatory. 
      * You already did the same for rightSide. 
      * This will take care of 1. 
      */ 
      System.out.printf("%7d", leftSide);   
     } 
     for (int rightSide = centerColumn; rightSide > 0; rightSide/=2){ 
      System.out.printf("%7d", rightSide); 
     } 
     /** Here we increment our counter with each iteration aka line that is printed */ 
     iteration++; 
     System.out.println(""); 
    } 
} 
+0

謝謝!感謝您花時間帶我瀏覽您的邏輯和徹底的解釋,包括我的代碼出了什麼問題。我被困在如何減少前面的空間。這種簡單的解決方案。我正在嘗試使用額外的某種類型的循環來執行遞減操作,但無法使其工作。 –

1

有沒有辦法可以後右和側打印左側。

Java從左到右打印。 (除非你使用一些阿拉伯編碼)

0

試試這個

編輯:先嚐試把你的空間(在這裏做我的第一個內部的循環)之前投入三角形你的電話號碼,然後塗抹一些邏輯在相應的位置打印號碼。

static DecimalFormat df = new DecimalFormat("#"); 
public static void main(String[] args) { 

    for (int i = 1; i < 10; i += 2) { 
     for (int k = 0; k < (4 - i/2); k++) { // first 
      System.out.print(" "); 
     } 

     for (int j = 0; j < i; j++) { // second 
      if (Math.pow(2, j) <= i) 
       System.out.print(df.format(Math.pow(2, j))); 
      else 
       System.out.print(df.format(Math.pow(2, i - j - 1))); 

     } 

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

我猜這會工作,但DF會拋出一個錯誤 - 我希望你可以通過你的邏輯走過我 - 或幫助我修改我的循環讓左側循環在右邊。非常感謝。 –

+0

@downvoters請評論,如果有什麼不對。 – amicngh

+0

@amicngh:downvote只是簡單地粘貼完成的代碼,只是添加了「嘗試這個」。這不會幫助提問者學習任何東西,甚至不是他的問題是什麼(這是:爲什麼他的循環不打印到左邊)。如果他想完成一段代碼,他可能會選擇你在網上找到的幾千個這種非常常見的編程練習中的一個。總之:只是一個不好的答案imho。 –

-1

我覺得你的方法不好。試試這個算法:

public class Main { 
    public static void main(String[] args) { 

     int levels = 8; 

     int lastLevelWidth = levels*2 + 1; 

     for (int i=0; i<levels; i++) { 
      int blankPositions = (lastLevelWidth - 1)/2 - i; 

      for (int j=0; j<blankPositions; j++) 
       print(" "); 

      int levelWidth = i*2 + 1; 
      int numberPositionsPerSide = (levelWidth-1)/2; 

      for (int j=0; j<numberPositionsPerSide; j++) { 
       print(Math.pow(2, j)); 
      } 

      print(Math.pow(2, i)); 

      for (int j=numberPositionsPerSide-1; j>=0; j--) { 
       print(Math.pow(2, j)); 
      } 

      System.out.println(); 
     } 
    } 

    public static void print(String string) { 
     System.out.print(string); 

     int spaces = 5 - string.length(); 

     for (int i=0; i<spaces; i++) 
      System.out.print(" "); 
    } 

    public static void print(double n) { 
     print(Integer.toString((int)n)); 
    } 
} 
+0

任何幫助我現有的代碼將不勝感激。我需要將我目前結構中的邏輯聯繫起來。 –

+0

你沒有關於你在做什麼的敘述......雖然我更喜歡修改我原來的代碼,所以我可以在'這個特殊情況'中看到我做錯了什麼......-如果你可以在你的評論中代碼或提供任何敘述你的代碼,爲什麼它會更好,這將不勝感激。 –