How to make a diamond using Nested For Loops

后端 未结 15 1285
终归单人心
终归单人心 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:53

    class Inc_Dec
    {
        public static void main(String[] args)
        {
           int le=11;int c=0;int j1=(le/2)+1;int j2=le-j1;
           for(int i=1;i<=le;i++)
           {
               if(c0;k--)
                 {
                  System.out.print(" ");
                 }  
                 for(int j=1;j<=i;j++)
                 {
                      System.out.print("*"+" ");
                 }
                 c++;
                 System.out.println();
               }
               else
               {
                   for(int k=(i-j1);k>0;k--)
                   {
                       System.out.print(" ");
                   }  
                   for(int j=(le-i+1);j>0;j--)
                   {
                       System.out.print("*"+" ");
                   }
                   System.out.println();
               }
           }
        }
    }
    

提交回复
热议问题