【问题标题】:How to have a sequence of output using While Loop in Java? [duplicate]如何在 Java 中使用 While 循环获得一系列输出? [复制]
【发布时间】:2016-11-02 10:01:46
【问题描述】:

我对如何应用代码感到困惑,我的输入是;

input: 5

我期待使用 while 循环得到这种序列输出;

input: 
5
output:
*****
****
***
**
*

我应用了这个代码;

int input ;
    String output = "*";

    Scanner sc = new Scanner(System.in);
    System.out.println ("input:");
    input = sc.nextInt();
    System.out.println ("output: ");
    while (input!=0){
        System.out.print (output);
        input--;

    }

但输出是;

input:
3
output: 
***

【问题讨论】:

  • 先做一些研究。这不能使用一个循环来完成。改用嵌套循环
  • 请可行

标签: java loops while-loop output


【解决方案1】:

您可以使用 for 循环尝试该代码。您无法通过使用一个循环来实现。

public class TmpTest {
    public static void main(String args[]) {
        int input;
        Scanner sc = new Scanner(System.in);
        input = sc.nextInt();
        for(int i=0;i<input;i++){
            for(int j = i; j<input;j++){
                System.out.print("*");
            }
            System.out.println("");
        }
     }
}

【讨论】:

    【解决方案2】:

    对于input 将具有的每个不同值,您需要另一个循环来打印多个output 标记,对应于input 的当前值。

    每次input 获得一个新值时,您还需要一个额外的新行(即:一旦我们即将运行一个新的for 循环)。

        while (input!=0){
    
            for(int i = 1;i <= input;i++)
            {
                System.out.print(output);
            }
    
            input--;
            System.out.println();
        }
    

    【讨论】:

      【解决方案3】:

      使用下面的代码,它会为你工作。

      public static void main(String[] args) {
              Scanner sc = new Scanner(System.in);
              System.out.println ("input:");
              int input = sc.nextInt();
              int outerLoopIndex = input; 
              System.out.println ("output: ");
              while (outerLoopIndex > 0) {
              int innerLoopIndex = 1;
                  while (innerLoopIndex < (outerLoopIndex + 1)) {
                      System.out.print("*");
                      innerLoopIndex++;
                  }
                  outerLoopIndex--;
                  System.out.println(" ");
              }
      
          }
      

      【讨论】:

        【解决方案4】:
            Scanner sc = new Scanner(System.in);
            System.out.println("input:");
            int n = sc.nextInt();
            int i = n;
            int j = 0;
            while (i != 0) {
        
                while (j != i) {
                    System.out.print("*");
                    j++;
                }
                j = 0;
                System.out.println("\n");
                i--;
            }
        

        【讨论】:

          【解决方案5】:
          public static void main(String[] args) {
              Scanner sc = new Scanner(System.in);
              System.out.println("input:");
              int n = sc.nextInt();
              int j = 0;
              recursive(n, j);
          }
          
          public static void recursive(int i, int j) {
              while (i != 0 && j != i) {
                  System.out.print("*");
                  j++;
          
              }
              j = 0;
              System.out.println("\n");
              if(i!=0)
              recursive(--i, j);
          
          }
          

          【讨论】:

          • 单循环试试这个,使用递归函数调用逻辑
          猜你喜欢
          • 2021-12-13
          • 2021-11-02
          • 2013-06-15
          • 2015-12-31
          • 1970-01-01
          • 1970-01-01
          • 2021-06-04
          • 1970-01-01
          • 2019-03-08
          相关资源
          最近更新 更多