2013-11-21 6 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(); 
    } 
} 
Смежные вопросы