我目前正在编写一个 java 代码,它从用户那里获取输入并输出 arraylist 的大小、输入的数字总和、输入的平均值和最大数量。我无法完成总和,因为 for 循环没有计算结果。我将不胜感激任何建议。我的代码如下。
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
double size = 0;
double a = -999;
double total = 0;
Scanner in = new Scanner(System.in); // scanner object for user input
ArrayList<Double> inputs = new ArrayList<Double>();
System.out.println("Enter a number, to terminate enter -999 :");
while (in.hasNextDouble()) {
//assign the nextDouble to a variable
double tmp = in.nextDouble();
//test the variable
if (tmp != a) {
//add the variable
//if you want to add -999 to the inputs then this next line above the test.
inputs.add(tmp);
System.out.println("Enter a number, to terminate enter -999 :");
} else {
inputs.size(); // get size of input
System.out.println("Numbers entered: " + inputs.size());
break;
}
}
for (double sum : inputs) {
total += sum;
System.out.println("The sum of numbers entered is " + total);
break;
}
}
}
慕勒3428872
相关分类