如何用星号绘制钻石

时间:2013-11-21 19:06:59

标签: java geometry draw

对于我的任务,我应该使用方法用星号画一个钻石。

我想出了如何制作第一部分(居中的三角形)

我不能因为上帝的爱而弄明白。我花了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 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分: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();
    }
}