【Java】用键盘输入若干数字,以非数字字符结束,计算这些数的和和平均值
package com.company; import java.util.*; public class Main { public static void main(String arg[]) { Scanner input = new Scanner(System.in); double sum = 0; int m = 0; while (input.hasNextDouble()) { double x=input.nextDouble(); m++; sum+=x; } System.out.printf("%d个数的和为%f\n",m,sum); System.out.printf("%d个数的平均值为%f\n",m,sum/m); } }
本文来自博客园,作者:木子欢儿,转载请注明原文链接:https://www.cnblogs.com/HGNET/p/14584469.html