【问题标题】:Number Patterns using loops in Java在 Java 中使用循环的数字模式
【发布时间】:2014-03-22 01:57:53
【问题描述】:

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

模式 1

54321
5432
543
54
5

模式 2

    1
   12
  123
 1234
12345

模式 3

 12345
  2345
   345
    45
     5

模式 4

  1
 123
12345
 123
  1

我几乎匹配模式 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");
}

【问题讨论】:

标签: java algorithm loops


【解决方案1】:
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();
    }
}

}

【讨论】:

  • 您是否考虑过解决方案的代码转储如何帮助 OP 理解手头的问题并在未来使用该语言提供的工具编写更好的程序?
  • 多谢多愁善感,看到你的代码其实是我在学习的方式。我目前正在查看它的工作原理,所以它确实有帮助。
  • 同意 MichaelIT 的观点。我只是认为如果 OP 是新的 JAVA 学习者,正确的代码可能会有所帮助。
【解决方案2】:

你的内部 for 循环

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

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

【讨论】:

    【解决方案3】:

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

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

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

    54321
    5432
    543
    54
    5
    

    【讨论】:

      【解决方案4】:

      试试这个:

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

      }

      【讨论】:

      • 请补充说明
      • 嗨,解释如下:我首先为输出的前三行编写了 for 循环:for (int i = 1; i = i; k--) { System.out.print(" "); } for (int j = 1; j
      【解决方案5】:
      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();
      
              }
          }
      

      【讨论】:

        【解决方案6】:

        第一个程序是:

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

        第二个程序是:

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

        【讨论】:

          【解决方案7】:
          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(" ");
                      }}}
          

          【讨论】:

          • 输出是您问题中的模式 1,54321 5432 543 54 5
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-10
          • 2015-06-26
          • 2013-05-09
          • 2013-04-24
          相关资源
          最近更新 更多