簡體   English   中英

java中使用數字的模式

[英]Pattern in java using numbers

我正在嘗試打印以下模式,但沒有得到邏輯如何.. 你能幫我一下嗎.. 我正在使用 BlueJ,這是我的第一個問題,所以我不確定需要什么。

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

先感謝您。

我試過這個

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();
        }
    }
}

但只能得到

                1
                22
                333
                4444
                55555

由於這似乎是作業,看看你是否能設法用這些數字構建一個直角三角形,所以1在上面,2 2在它下面,4 3在它下面,等等:

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

一旦你管理的是,所有你需要做的是找出你需要多少空白值每個號碼前加。

你可以試試這段代碼,因為它會先打印空格,然后是你的號碼。 而且每個數字后面都有空格。

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

        }

嘗試這個:

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