Java-在 while 循环中存储值

Java-Storing values in a while loop

我是 Java 的初学者,我有一个关于循环的作业。我知道这个问题对您来说可能听起来很基础,但我找不到可以理解的答案。我的代码基本上是这样的;

import java.util.Scanner;

public class Histogram
{
  public static void main( String[] args)
  {
    Scanner scan = new Scanner( System.in);

    // Constant

    final String CH = "*";

    // Variables
    int number;
    int count;
    int start;
    int width;
    int line;
    String output;


    // Program Code

    count = 0;
    number = 0;
    start = 1;
    width = 0;
    line = 0;
    output = "";


    // Creating the loop

    while ( start == 1) 
    {
      System.out.println( "Enter the numbers");
      number = scan.nextInt();

      if ( number < 0 )
      {
        start = 0;
      }

      else
      {  
        while ( width < number)
        {
         output = CH + " " ;
         width = width + 1  ;  
         System.out.print(output);
        }
        width = 0;


      }
    }

  }
}

这运行完美,但每次用户输入后都会打印星星的输出。我想存储每个循环的输出,并在输入负整数时在最后一起打印它们。如何存储我不知道用户将输入多少输出的输出? 简而言之,我想要以下输出

Enter the numbers : 3 5 6 4 -1 //numbers are user input

3 *** 5 ***** 6 ****** 4 ****

使用列表,例如 ArrayList,而不是数组,因为大小可以动态增长。

例如,

List<Integer> ints = new ArrayList<>();
ints.add(5);
ints.add(2);
ints.add(7);

for(Integer i : ints){
  System.out.println(i);
}

如果您不想重复,可以改用 Set。

您只需在 while 范围之外创建一个变量(类似于 StringStringBuilder (首选)),而不是直接打印你的结果,你会附加任何输出,到这个变量并在最后打印它。

使用这种方法,您基本上是收集整个输出并一次性打印出来。

...

//create a output variable outside while loop. You can also use simple String
StringBuilder out = new StringBuilder();

while ( start == 1) 
{
  System.out.println( "Enter the numbers");
  number = scan.nextInt();

  if ( number < 0 )
  {
    start = 0;
  }

  else
  {  
    while ( width < number)
    {
     output = CH + " " ;
     width = width + 1  ;  

     //instead of directly printing, you can append your output to out variable.
     out.append(output);

    }
    width = 0;


  }
}

//after coming out of the loop, you can now print it
System.out.print(out);

...

在 "else" 中,您可以只存储输入的数字:

List<Integer> myList = new ArrayList<>(); //previously defined
(...)
else {
      myList.add(number);
}

然后在 while 循环之外,填充数组列表后:

for(int i=0;i<myList.size();i++)
{
     for(int j=0;j<myList.get(i);j++)
     {
         System.out.print("*");
     }
     System.out.print("\n");
}

这是答案:

import java.util.Scanner;

public class Histogram
{
  public static void main( String[] args)
  {
    Scanner scan = new Scanner( System.in);

    // Constant

    final String CH = "*";



   int count = 0;
    int number = 0;
    int start = 1;
    int width = 0;
    int line = 0;
    String output = "";
    String store="";


    // Creating the loop

    while ( start == 1) 
    {
      System.out.println( "Enter the numbers");
      number = scan.nextInt();

      if ( number < 0 )
      {
        start = 0;
      }

      else
      {  
        while ( width < number)
        {
         output = CH + " " ;
         width = width + 1  ;  
        store+=output;
        }
        width = 0;


      }
    }
    System.out.print(store);

  }
}