问题描述
对于我的作业,我应该使用方法绘制带有星号的钻石。
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)
我无法为上帝的爱弄清楚。我花了4个多小时尝试各种不同的事情,并且弄清楚了如何制作一个倒置三角形,但是钻石却无法解决问题。
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--;
}
}
这是它显示的内容(UserInputHeight = 10):
This is what it displays (UserInputHeight = 10):
*
***
*****
*******
*********
***********
这就是我想要的(UserInputHeight = 19):
This is what I want (UserInputHeight = 19):
*
***
*****
*******
*********
***********
***********
*********
*******
*****
***
*
我就是这样远至第二部分:
This is what I have so far for the second part:
{
int行= userInputHeight;
{ 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;
}
}
请帮助。
推荐答案
尝试一下:
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();
}
}
这篇关于如何用星号绘制钻石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!