-2

我需要输入 n 个数字,将它们存储在一个变量中,并使其可供以后处理。约束: 1. 连续输入之间的任意数量的空格。2. 输入的数量是未知的。3.输入集不应超过256KB,应在0<= i <=10^18之间

Example Input: 
100
                  9 
        81
                              128
1278
4

2 回答 2

2

If I understand your question, then yes. One way, is to use a Scanner, and hasNextDouble()

Scanner scan = new Scanner(System.in);
List<Double> al = new ArrayList<>();
while (scan.hasNextDouble()) { // <-- when no more doubles, the loop will stop.
  al.add(scan.nextDouble());
}
System.out.println(al);
于 2014-09-12T17:11:40.177 回答
0

如果您的输入都像您的文本一样在一行中输入,您可以执行以下操作:

import java.util.ArrayList;

public class Numbers 
{
    public static void main(String[] args) 
    {
        ArrayList<Double> numbers = new ArrayList<Double>();
        String[] inputs = args[0].split(" ");

        for (String input : inputs)
        {
            numbers.add(Double.parseDouble(input));
        }
        //do something clever with numbers array list.
    }
}
于 2014-09-12T17:15:11.610 回答