【问题标题】:How can I print the following pattern using nested for loops in JAVA?如何使用 JAVA 中的嵌套 for 循环打印以下模式?
【发布时间】:2018-12-04 05:50:37
【问题描述】:

我无法使用 for 循环在 Java 程序中打印以下模式。 请就此问题寻求帮助。

    5
   54
  543
 5432
54321

代码

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

【问题讨论】:

  • 请贴出你写的代码来尝试解决这个问题
  • Stack Overflow 不是免费的编码作业服务。请向我们展示您已经尝试过的代码。
  • 您好,欢迎来到 StackOverflow。请花一些时间阅读帮助页面,尤其是名为"What topics can I ask about here?""What types of questions should I avoid asking?" 的部分。更重要的是,请阅读Stack Overflow question checklist。您可能还想了解Minimal, Complete, and Verifiable Examples。格式化您的问题,并向我们展示您迄今为止的工作。
  • 导入 java.util.Scanner;公共类 PattrenClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //从用户获取行值 System.out.println("你想要这个模式有多少行?"); int 行 = sc.nextInt(); System.out.println("这是你的模式……!!!"); for (int i = rows; i >= 1; i--) { for (int j = 1; j
  • edit您的问题包含代码。 (在 cmets 中读起来有点困难)

标签: java for-loop


【解决方案1】:

试试这个代码,

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

它打印输入 5,

你想要这个模式有多少行? 5

    5
   45
  345
 2345
12345

【讨论】:

    【解决方案2】:

    在您当前的代码中,您只是打印空间。现在必须更进一步,将数字与新行一起打印。

    您可以按照以下方式进行操作。 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(); 
                }
            }
        }
    }
    

    输出

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

    【讨论】:

    • 谢谢......它工作......非常感谢你帮助我......实际上我正在学习用 JAVA 编码......这是提出疑问的最佳平台。跨度>
    【解决方案3】:

    此代码有效....我自己测试过。如有任何疑问,请随时提出?。

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

    您可以在下面看到 BlueJ 终端窗口屏幕截图 -

    Terminal Window of BlueJ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      相关资源
      最近更新 更多