【问题标题】:Pyramid Pattern using a Loop使用循环的金字塔模式
【发布时间】:2013-05-09 09:31:44
【问题描述】:

预期的输出如下:

1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012

以下是我将使用的起始代码:

import java.util.Scanner;

public class Pyramid {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Type in an integer value");
        Scanner in = new Scanner(System.in);
        int input = in.nextInt();
        String str = "";
        for(int i=1;i<=input;i++){
            str += i;
            System.out.println(str);
        }
    }
}

以下是我目前的输出。

Type in an integer value
15
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
1234567891011
123456789101112
12345678910111213
1234567891011121314
123456789101112131415

我一直在思考如何解决这个问题;如果我写一个 If 语句if(i &gt; 9){ i = 0; }。但这会重置我的计数器吗?

我怎样才能完成这项任务? 我错过了什么?

【问题讨论】:

    标签: java variables if-statement for-loop auto-increment


    【解决方案1】:

    您可以使用模运算符使i 在达到 10 时循环回 0:

    str += i % 10;
    

    【讨论】:

    • 我不知道为什么我没有想到这一点......这很有意义:)
    【解决方案2】:

    您只需要使用% 模运算符,就可以处理任何数字。

    public static void main(String[] args) throws IOException {
        System.out.println("Type in an integer value");
        Scanner in = new Scanner(System.in);
        int input = in.nextInt();
        String str = "";
        for (int i = 1; i <= input; i++) {
            str += i % 10;
            System.out.println(str);
        }
    }
    

    【讨论】:

    • 你为什么要使用System.exit(0); 真的需要吗?
    • 你为什么要使用throws IOException 真的需要吗?
    • 不 System.exit(0) 表示 0 = false ; 1 = 真?接受一个布尔值作为参数?
    【解决方案3】:

    解决方案:

    IntStream.rangeClosed(1, MAX)
                    .forEach(i -> IntStream.rangeClosed(1, i)
                            .mapToObj(j -> j == i ? j % 10 + "\n" : j % 10 + " ")
                            .forEach(System.out::print)
                    );
    

    【讨论】:

      【解决方案4】:
      public class counter1 
      {                           
          public static void main(String[] args) 
          {// TODO Auto-generated method stub
              while(true)                                 //keep repeating code
              {
                  System.out.println("Enter Number...");
                  Scanner number = new Scanner(System.in);
                  int repeat = number.nextInt();          //input number
                  int c = 1;                              //start counting at 1
                  while (c<=repeat)                       //count vertically if this if true
                  {
                      int a = 1;                          //always start each count from 1
                      do
                      {
                          System.out.print(a + " ");      //print number and a space horizontally
                          a++;                            //increment horizontal numbers by 1
                      }   while (a<=c);                   //check if the final number on          horizontal line is less max, if max-exit loop
                      c++;                                    
                      System.out.println();               //line break
                   }
               }
      
          }
      }
      

      【讨论】:

      • 首先:你为什么要回答一个已经很好回答的老问题?此外,您使用不带break; 语句的while(true) {...} 来逃避此循环。但是:您的代码格式不正确,并且您使用了很多不需要的循环。
      • 感谢您格式化电子代码。只是使用最基本的语句发布另一种方法。三天前我在课堂上被问到同样的问题,老问题,但仍然被一遍又一遍地问到。就像“你好吗?”,老问题,我们仍然回答
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多