简体   繁体   English

java中使用数字的模式

[英]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.我正在尝试打印以下模式,但没有得到逻辑如何.. 你能帮我一下吗.. 我正在使用 BlueJ,这是我的第一个问题,所以我不确定需要什么。

                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在它下面,4 3在它下面,等等:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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