How to draw diamond with asterisks

2019-08-30 11:04发布

问题:

For my assignment I am supposed to make a Draw a diamond with asterisks using methods.

I figured out how to make the first part (centered triangle)

I cannot for the love of God figure it out. I have spent over 4 hours trying different things and I figured how to make an upside down triangle, but the diamond is not working out.

This is what I have for the first part. Can someone tell me how to flip it so that it will form a diamond when used with an upside down version?

{
    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--;
    }
}

This is what it displays (UserInputHeight = 10):

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




This is what I want (UserInputHeight = 19):

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







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

This is what I have so far for the second part:

{ 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;
        }
    }

Please help.

回答1:

Try this:

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();
    }
}


回答2:

Here is another angle of looking at it.

Note: height strands from the mid-line to the uppermost point.

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();
    }
}