我无法制作带有循环的菱形图案 [英] I'm having trouble making a diamond shape with loops

查看:136
本文介绍了我无法制作带有循环的菱形图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您有一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆