How to make a diamond using Nested For Loops

后端 未结 15 1284
终归单人心
终归单人心 2021-01-01 07:09

So I was assigned to make a diamond with asterisks in Java and I\'m really stumped. Here\'s what I\'ve come up with so far:

public class Lab1 
{
   public s         


        
15条回答
  •  攒了一身酷
    2021-01-01 07:37

    I can see what you are trying to do and this is a pretty neat way to think about the diamond.

    You will have some issues with the j counter when i goes negative..look at how to use Math.abs()

    Also try writing some pseudo code in basic steps with comments to get the pattern clear:

     //print 5 spaces + 1 star
     //print 4 spaces + 2 stars
     //print 3 spaces + 3 stars
     //print 2 spaces+ 4 stars
     .
     . 
     .  
     //print 5 spaces + 1 star
    

    Then, literally substitute variables (j and i) for the numbers.

    You now have a model. This is often the hardest part in programming..getting the model right. Only jump into coding when you have a good idea for how the model works.

    Once you have the variables substituted, you can try to convert the whole thing into an automated loop.

提交回复
热议问题