2013-11-21 89 views
0

對於我的任務,我應該使用方法繪製帶星號的鑽石。如何繪製帶星號的鑽石

我想通了,如何讓第一部分(居中三角形)

我不能代表上帝的愛看着辦吧。我花了4個多小時嘗試不同的事情,我想如何製作一個顛倒的三角形,但鑽石不工作。

這就是我的第一部分。有人可以告訴我如何翻轉它,以便在與顛倒的版本一起使用時形成鑽石?

{ 
    int rows = userInputHeight; 

    int starCount = 1; 
    int spaceCount = rows - 1; 

    for(int rowCount = 1; rowCount <= rows; rowCount++) 
    { 
     for(int numb = 1; numb <= spaceCount; numb++) 
     { 
      System.out.print(" "); 
     } 
     for(int count = 1; count <=starCount; count++ ) 
     { 
      System.out.print("*"); 
     } 
     System.out.println(); 
     starCount += 2; 
     spaceCount--; 
    } 
} 

這是它顯示(UserInputHeight = 10):

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




這就是我想要的(UserInputHeight = 19):

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







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

這是我到目前爲止第二部分:

int rows = userInputHeight;

 int starCount = rows*2; 
     int spaceCount =userInputPadding; 

     if (userInputHeight % 2 == 0) 
     { 
      userInputHeight+=1; 
     } 
     for (int rowCount = rows; rowCount >= 1; rowCount --) 
     { 
      for (int i = 0; i <= (rows - rowCount)+ spaceCount; i++) 
      { 
       System.out.print(' '); 
      }  
      for (int i = 1; i < starCount; i++) 
      { 
       System.out.print('*'); 
      } 
      System.out.println(); 
      starCount -=2; 
     } 
    } 

請幫忙。

+2

javascript在什麼地方出現? – mplungjan

+1

您最後一次打印的方法是什麼? –

回答

1

試試這個:

public static void drawDiamond(int height) { 
    if (height % 2 == 0) throw new AssertionError("Height should be an odd number!"); 
    height = (height + 1)/2; 
    drawTop(height); 
    drawBot(height - 1); 
} 

public static void drawTop(int height) { 
    int rows = height; 
    int starCount = 1; 
    int spaceCount = rows - 1; 
    for (int rowCount = 1; rowCount <= rows; rowCount++) { 
     for (int i = 0; i < spaceCount; i++) { 
      System.out.print(" "); 
     } 
     for (int i = 0; i < starCount; i++) { 
      System.out.print("*"); 
     } 
     starCount += 2; 
     spaceCount--; 
     System.out.println(); 
    } 
} 

public static void drawBot(int height) { 
    int rows = height; 
    int starCount = 2 * (rows - 1) + 1; 
    int spaceCount = 1; 
    for (int rowCount = 1; rowCount <= rows; rowCount++) { 
     for (int i = 0; i < spaceCount; i++) { 
      System.out.print(" "); 
     } 
     for (int i = 0; i < starCount; i++) { 
      System.out.print("*"); 
     } 
     starCount -= 2; 
     spaceCount++; 
     System.out.println(); 
    } 
} 
0

這裏是看它另一個角度。

注意:高度從中線到最高點。

public static void DrawDiamond(int height) 
{ 
    DiamondTop(height); 
    DiamondBottom(height); 
} 

public static void DiamondTop(int height) 
{ 
    for (int row = 1; row <= height; row++) 
    { 
     for (int padding = height - row; padding > 0; padding--) 
     { 
      System.out.print(" "); 
     } 

     for (int numberOfAsterisks = (row * 2) - 1; numberOfAsterisks > 0; numberOfAsterisks--) 
     { 
      System.out.print("*"); 
     } 
     System.out.println(); 
    } 
} 

public static void DiamondBottom(int height) 
{ 
    for (int row = height - 1; row > 0; row--) 
    { 
     for (int padding = row; padding < height; padding++) 
     { 
      System.out.print(" "); 
     } 

     for (int numberOfAsterisks = (row * 2) - 1; numberOfAsterisks > 0; numberOfAsterisks--) 
     { 
      System.out.print("*"); 
     } 
     System.out.println(); 
    } 
}