Scanner实现求和和均值
public static void main(String[] args) {
//输入多个数字,求和和平均数
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m = 0;
//当输入的不是一个double类型就会结束循环
while (scanner.hasNextDouble()){
double x = scanner.nextDouble();
sum += x;
m++;
}
System.out.println(m + "个数的和" + sum);
System.out.println(m + "个数的平均值为" + (sum / m));
scanner.close();
/*控制台如下
10
20
30
kk
3个数的和60.0
3个数的平均值为20.0
*/
}