简体   繁体   中英

Pattern in java using numbers

I am trying to print the following pattern but don't get the logic how to.. Can you please help me out.. I am using BlueJ and this is my first question so I am not sure what is required.

                1
               2 2
             3 3 3 3
           4 4 4 4 4 4

Thank You in advance.

i have tried this

public class Program92
{
    public static void main()
    {
        for(int x=1;x<=5;x++)
        {
            for(int j=1;j<=x;j++)
             System.out.print(x);
            System.out.println();
        }
    }
}

but could only get

                1
                22
                333
                4444
                55555

Since this seems to be homework, see if you can manage to get it to build a right angled triangle with those numbers, so 1 on top, 2 2 's beneath it, 4 3 's beneath that, etc:

1
2 2
3 3 3 3
4 4 4 4 4 4

Once that you manage that, all that you would need to do would be to figure out how many white space values you need to add before each number.

you can try this snippet of code as it will print first blank space then your number. and also after every number there is blank space.

public static void main(String[] args) {
    int l= 5;int k=0;
     for(int x=1;x<5;x++)
        {
         for(int i=l*2-1;i>0;i--)
         {  
            if(x == 1 && i ==1)
                break;
            System.out.print(" "); 
         }
         System.out.print(x); 
         System.out.print(" "); 
         for(int i=1;i<x*2-2;i++)
         {
             System.out.print(x); 
             System.out.print(" "); 
         }
         System.out.println();
         l--;
        }

        }

Try this:

public class program98
{
public static void main()
{
System.out.print("     "+"1");//5 spaces int the blank
for(int i=1;i<=4;i++)
{
 for(int s=6;s>1;s--)
   {System.out.print(" ");//1 space
    }
  for(int j=1;j<1;j++)
    {System,out.print(i);
     }
    for(int j=1;j<1;j++)
      {System.out.print(i);//prints this twice. Hence,instead of once,the number of times it prints is double
       }
         System.out.println(" ");//1 space
        }
       }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM