简体   繁体   English

如何使用 JAVA 中的嵌套 for 循环打印以下模式?

[英]How can I print the following pattern using nested for loops in JAVA?

I am unable to print the following pattern in Java program using for-loops.我无法使用 for 循环在 Java 程序中打印以下模式。 Kindly request help in this matter.请就此问题寻求帮助。

    5
   54
  543
 5432
54321

Code代码

    Scanner sc = new Scanner(System.in); // Taking rows value from the user
    System.out.println("How many rows you want in this pattern?");
    int rows = sc.nextInt();
    System.out.println("Here is your pattern....!!!");
    for (int i = rows; i >= 1; i--) {
        for (int j = 1; j < i; j++) {
            System.out.print(" ");
        }
    }

Try this code,试试这个代码,

public static void main(String args[]) throws Exception {
    try (Scanner sc = new Scanner(System.in);) { // Taking rows value from the user
        System.out.println("How many rows you want in this pattern?");
        int rows = sc.nextInt();
        if(rows <=0) {
            System.out.println("Please enter a positive number only.");
            return;
        }
        for (int i = 0; i < rows; i++) {
            for (int j = rows; j > 0; j--) {
                if (j <= i + 1) {
                    System.out.print(rows - j + 1);
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

For input 5 it prints,对于输入 5,它打印,

How many rows you want in this pattern?您想要这种模式中的多少行? 5 5

    5
   45
  345
 2345
12345

In your present code, you are just printing space.在您目前的代码中,您只是在打印空间。 Now to have to go one step further and print the Numbers along with a new line.现在必须更进一步,打印数字和新行。

You can do it as following.您可以按照以下方式进行。 See it working here :看到它在这里工作

public class PattrenClass 
{ 
    public static void main(String[] args) 
    { 
        //Connecting Keyboard to Scanner with `try-with-resources`
        try(Scanner sc = new Scanner(System.in);)
        {
            System.out.println("How many rows you want in this pattern?");
            int rows = sc.nextInt(); //Taking rows value from the user
            System.out.println("Here is your pattern....!!!");
            for (int i = rows; i > 0; i--)
            { 
                for (int j = 1; j < i; j++) 
                { 
                    System.out.print(" "); 
                }
                for (int j = rows; j >= i; j--) 
                { 
                    System.out.print(j); 
                }
                System.out.println(); 
            }
        }
    }
}

OUTPUT :输出

How many rows you want in this pattern?
5
Here is your pattern....!!!
    5
   54
  543
 5432
54321

This code is working....I've tested it my self.这段代码正在工作......我已经自己测试过了。 Feel free to ask any further doubts😃.如有任何疑问,欢迎随时提问😃。

import java.util.Scanner;
public class pattern_54321
{
    public static void main (String args[])
    {
        Scanner sc=new Scanner (System.in);
        System.out.println("Enter the number of rows -");
        int r=sc.nextInt();
        System.out.println("Here is your required pattern -");
        int i=0,j=0,k=0,n=r;
        for(i=r;i>=1;i--)
        {
            for(k=n;k>=1;k--)
            {
                System.out.print(" ");
            }
            for(j=r;j>=i;j--)
            {
                System.out.print(j);
            }
            System.out.println();
            n--;
        }
    }
}

You can see the BlueJ Terminal Window Screenshot below -您可以在下面看到 BlueJ 终端窗口屏幕截图 -

Terminal Window of BlueJ BlueJ 的终端窗口

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

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