gpt4 book ai didi

java - 将字符串数组转换为 double 组

转载 作者:行者123 更新时间:2023-12-01 10:16:32 31 4
gpt4 key购买 nike

我知道这里有很多关于这个问题的问题,但我做了所有这些解决方案,但它仍然不起作用。由于某种原因它只存储第一个输入

我的输入:

1
2
3
4

预期输出:

[1.0,2.0,3.0,4.0]

实际输出:

[1.0]

代码

inputValue = stdin.readLine (); //Reads input
String [] input = inputValue.split ("\\R");
double [] numbers = new double[input.length];
for(int i = 0; i < input.length; i++){
numbers[i] = Double.parseDouble(input[i]);}

最佳答案

输入文件包含 4 个单独的行。

readline() 只读取一行

您只读取并处理了一行。这会将所有输入处理到 ArrayList 中,如果您确实需要,稍后可以将其转换为数组。

List<Double> numbers = new ArrayList<>();
while( (inputValue = stdin.readLine()) != null) {
numbers.add(Double.valueOf(inputValue);
}

关于java - 将字符串数组转换为 double 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35858785/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com