在 Java 中使用循环的数字模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22572617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:33:50  来源:igfitidea点击:

Number Patterns using loops in Java

javaalgorithmloops

提问by dev

I have been trying different variations of for loops and have no clue how to make these patterns:

我一直在尝试 for 循环的不同变体,但不知道如何制作这些模式:

Pattern 1

模式一

54321
5432
543
54
5

Pattern 2

模式2

    1
   12
  123
 1234
12345

Pattern 3

模式3

 12345
  2345
   345
    45
     5

Pattern 4

图案 4

  1
 123
12345
 123
  1

My code that almost matched pattern 1 is the following but doesn't work like the example above.

我几乎匹配模式 1 的代码如下,但不像上面的例子那样工作。

for (int i = 1 ; i <= rows ; i++) {
    for (int j = (rows + 1 - i) ; j  > 0 ; j-- ) {
        System.out.print(j);
    }   
    System.out.print("\n");
}

采纳答案by Sentimental

public class PrintPattern {

public static void main(String[] args){
    printPattern1();
    printPattern2();
    printPattern3();
    printPattern4();
}

public static void printPattern1(){

    for (int i = 0; i<5; i++){
        for(int j = 5; j>i; j--)
            System.out.print(j);
        System.out.println();
    }
}

public static void printPattern2(){

    for (int i = 0; i<5; i++){
        for(int k = 0; k<4-i; k++)
            System.out.print(" ");
        for(int j = 1; j<=i+1; j++)
            System.out.print(j);
        System.out.println();
    }
}

public static void printPattern3(){

    for (int i = 0; i<5; i++){
        for(int k = 0; k<i; k++)
            System.out.print(" ");
        for(int j = i+1; j<=5; j++)
            System.out.print(j);

        System.out.println();
    }
}

public static void printPattern4(){

    for (int i = 0; i<5; i++){
        for(int k = 0; k<Math.abs(2-i); k++)
            System.out.print(" ");
        for(int j = 1; j<=5-2*Math.abs(2-i); j++)
            System.out.print(j);
        for (int p = 0; p<Math.abs(2-i); p++)
            System.out.print(" ");
        System.out.println();
    }
}

}

}

回答by mikebolt

Your inner for loop

你的内部 for 循环

for (int j = (rows + 1 - i) ; j  > 0 ; j-- ){
    System.out.print(j);
}

will always count down to 1, because it keeps going until j is zero. Also, the number that the current row starts on will depend on the current row, because you used i in your assignment to j. To get pattern 1 both of those things will have to change.

将始终倒数到 1,因为它会一直持续到 j 为零。此外,当前行开始的编号将取决于当前行,因为您在分配给 j 时使用了 i。要获得模式 1,这两件事都必须改变。

回答by Elliott Frisch

Since you posted an attempt for pattern one, I'll tell you a solution for pattern one -

既然你发布了模式一的尝试,我会告诉你模式一的解决方案 -

int rows = 5; // <- Start at 5.
for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s).
  for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again)
    System.out.print(j);
  }
  System.out.println();
}

Output is pattern 1 in your question,

输出是您问题中的模式 1,

54321
5432
543
54
5

回答by Gee858eeG

public static void main(String[] args) {

        int rows = 5;
        System.out.println("------ PATTERN 1 ------");

        for (int i = 1 ; i <= rows ; i++){
            for (int j = rows; j >= i ; j--){
                System.out.print(j);
            }
            System.out.println();

        }


        System.out.println("\n------ PATTERN 2 ------");

        for (int i = 1 ; i <= rows ; i++){
            int k;
            for (k = rows ; k > i; k--){
                System.out.print(" ");
            }
            for (int j = 1; j <= k ; j++){
                System.out.print(j);
            }
            System.out.println();

        }

        System.out.println("\n------ PATTERN 3 ------");


        for (int i = rows ; i >= 1 ; i--){
            int k;
            for (k = rows ; k > i; k--){
                System.out.print(" ");
            }
            for (int j = 1; j <= k ; j++){
                System.out.print(j);
            }
            System.out.println();   
        }

        System.out.println("\n------ PATTERN 4 ------");

        int whitespaces = rows/2;
        for (int i = 1 ; i <= rows; i++){
            // absolute value of whitespaces
            int abs_whitespaces = 
                    (whitespaces < 0 ? -whitespaces : whitespaces);
            for (int j = 0 ; j < abs_whitespaces ; j++){
                System.out.print(" ");
            }

            for (int j = 1 ; j <= rows - 2 * abs_whitespaces ; j++){
                System.out.print(j);
            }

            whitespaces-=1;
            System.out.println();

        }
    }

回答by anshul

The first program is:

第一个程序是:

class timepass {
    public static void main() {
        for (int a = -1;a<=5;a++) {
            for(int b = 5; b >= a;b--) {
                System.out.print("*");   
            }
            System.out.println();
            // enter code here
        }
    }
}

The second program is:

第二个程序是:

class timepass {
    public static void main() {
        for(int i = 1;i<= 6;i++) {
            for(int j = 1;j<= i ;j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

回答by Sohrab Alam

Try this:

尝试这个:

public class NumberPattern {
  public static void main(String[] args) {

    for (int i = 1; i <= 3; i++) {

        for (int k = 2; k >= i; k--) {
            System.out.print(" ");
        }

        for (int j = 1; j <= (2 * i - 1); j++) {
            System.out.print(j);
        }
        System.out.println();

    }
    for (int i = 1; i <= 2; i++) {

        for (int k = i; k > 0; k--) {
            System.out.print(" ");
        }

        if (i % 2 != 0) {

            for (int j = 1; j <= (2 * i + 1); j++) {
                System.out.print(j);
            }
        } else {
            for (int j = 1; j <= (i / 2); j++) {
                System.out.print(j);
            }
        }
        System.out.println();

    }
}

}

}

回答by Harish Yellapragada

import java.util.Scanner;

public class Triangle {
    public static void main(String[ ] args){

        Scanner scan = new Scanner(System.in);
        System.out.println("enter no.of line you need");
        int n = scan.nextInt();
            for(int i=1;i<=n;i++){
                for(int j=5;j>=i;j--){
                    System.out.print(j);
            }
                System.out.println(" ");
            }}}