Creating an hourglass using asterisks

后端 未结 2 1122
情歌与酒
情歌与酒 2021-01-16 16:38

I would like to create an hourglass using the \"*\" character. For example if the user input was 5 then it would look like this:

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


        
2条回答
  •  天涯浪人
    2021-01-16 17:22

    Pseudocode:

    function stars(n):
      c1 = 0
      c2 = n
      for i = 1 to n
        print ' ' * c1
        print '*' * c2
        if (i < (n+1) / 2):
          c1 ++;
          c2 -=2;
        else:
          c1--
          c2 +=2
        print newline
    

    This should get you close to your answer... now translate into java

    ** note - this works for odd number. You may need to tweak for even n **

提交回复
热议问题