I'm having trouble making a diamond shape with loops

穿精又带淫゛_ 提交于 2019-12-23 04:49:07

问题


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
   *--*                           *--*
  *----*                         *----*
 *------*                       *------*
*--------*                     *--------*
*--------*
 *------*
  *----*
   *--*
    **

回答1:


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



回答2:


public static void main(String[] args) {
        int n = 10;
        for (int i = 1 ; i < n ; i += 2) {
            for (int j = 0 ; j < n - 1 - i / 2 ; j++)
                System.out.print(" ");

            for (int j = 0 ; j < i ; j++)
                System.out.print("*");

            System.out.print("\n");
        }

        for (int i = 7 ; i > 0 ; i -= 2) {
            for (int j = 0 ; j < 9 - i / 2 ; j++)
                System.out.print(" ");

            for (int j = 0 ; j < i ; j++)
                System.out.print("*");

            System.out.print("\n");
        }
    }

output

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



回答3:


Since you have half the diamond already formed, simply run the loop again, in reverse, eg:

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

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



回答4:


Whenever I see a symmetry of a kind, recursions ring to my head. I'm posting only for you and others interesting into learning more. When beginning, recursions can harder to grasp but since you already have loop based solutions, contrasting against recursion will clearly outline advantages and disadvantages. My advice, don't miss out on the chance to get into it :)

A recursive solution:

static int iteration = 0;
public static void printDiamond(int n) {
    int numberOfBlanks = n - iteration;
    int numberOfDashes = iteration * 2;
    String blank = new String(new char[numberOfBlanks]).replace("\0", " ");
    String dash = new String(new char[numberOfDashes]).replace("\0", "-");
    String star = "*";
    String row = blank + star + dash + star + blank;

    // printing the rows forward
    System.out.println(row);
    iteration++;
    if (iteration < n) {
        printDiamond(n);
    }
    // printing the rows backward
    System.out.println(row);
}

first, don't get confused with the strange new String(new char[numberOfBlanks]).replace("\0", " "); its a neat trick in java to construct a string with repeated chars, eg new String(new char[5]).replace("\0", "+"); would create the following String +++++

The recursion bit explained. The second println won't run until the recursion is stopped. The stop criteria is defined by iteration < n. Up until that point the rows up until that point will be printed. So something like this:

iteration 1. row =     **     
iteration 2. row =    *--*    
iteration 3. row =   *----*   
iteration 4. row =  *------*  
iteration 5. row = *--------* 

than the recursion stops, and the rest of the code is executed but in reversed order. So only the second println is printed, and the value of row variable is like as follows

continuing after 5 iteration row =   *--------* 
continuing after 4 iteration row =    *------*  
continuing after 3 iteration row =     *----*   
continuing after 2 iteration row =      *--*    
continuing after 1 iteration row =       **

I didn't go into mechanics behind it, plenty of resource, this is just to get you intrigued. Hope it helps, best



来源:https://stackoverflow.com/questions/26459514/im-having-trouble-making-a-diamond-shape-with-loops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!