我无法制作带有循环的菱形图案 [英] I'm having trouble making a diamond shape with loops
问题描述
您有一个 n
的输入,它代表了钻石所有行的一半。我能够做出钻石的前半部分,但是我对下半场极为沮丧。我似乎无法得到它。我不是在这里问我需要的具体代码,但你能指出我在正确的方向,并给我一些技巧,如何写这个技巧?另外,如果我以错误的方式讨论这个计划,请随时告诉我并告诉我应该如何处理这个计划。
底部的钻石代表 5的输入。 n-1 表示每个星号左侧的空格。感谢您的帮助!
$ prefid =lang -java prettyprint-override>
public static void printDiamond(int n)
{ (int a = 0; a<(n-(i + 1)); a ++)$ b $为(int i = 0; i {
b {
System.out.print();
}
System.out.print(*);
for(int b = 0; b <(i * 2); b ++)
{
System.out.print( - );
}
System.out.print(*);
System.out.println();
**我需要什么**我目前有什么
* - * * - *
* ---- * * ---- *
* - ----- * * ------ *
* -------- * * -------- *
* ------- - *
* ------ *
* ---- *
* - *
**
解决方案只要将您的循环倒过来即可:
对于(int a = 0; a<(n-(i + 1))),(int i = n-1; i> = 0; i-)
{
> ); a ++)
{
System.out.print();
}
System.out.print(*);
for(int b = 0; b <(i * 2); b ++)
{
System.out.print( - );
}
System.out.print(*);
System.out.println();
}
You have an input of n
and that represents half the rows that the diamond will have. I was able to make the first half of the diamond but I'm EXTREMELY frustrated with the second half. I just can't seem to get it. I'm not here to ask for specific code I need, but can you point me in the right direction and give me some tips/tricks on how to write this? Also, if I'm going about this program the wrong way, feel free to tell me and tell me on how I should approach the program.
The diamonds at the bottom represent an input of 5. n-1 represents the spaces to the left of each asterisk. Thank you for your help!
public static void printDiamond(int n)
{
for(int i=0;i<n;i++)
{
for(int a=0;a<(n-(i+1));a++)
{
System.out.print(" ");
}
System.out.print("*");
for(int b=0; b<(i*2);b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
}
** What I need ** What I have currently
*--* *--*
*----* *----*
*------* *------*
*--------* *--------*
*--------*
*------*
*----*
*--*
**
Just reverse your loop :
for(int i=n-1;i>=0;i--)
{
for(int a=0;a<(n-(i+1));a++)
{
System.out.print(" ");
}
System.out.print("*");
for(int b=0; b<(i*2);b++)
{
System.out.print("-");
}
System.out.print("*");
System.out.println();
}
这篇关于我无法制作带有循环的菱形图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!