求和及求平均-while循环语句的应用-2022-11-2
package scanner;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0;
int m=0;
while (scanner.hasNextDouble()){
double x=scanner.nextDouble();
sum=sum+x;
m=m+1;
System.out.println("你输入了第"+m+"个数据,然后当前结果为"+sum);
}
System.out.println("共计输入了"+m+"个数据,总和为"+sum);
System.out.println("共计输入了"+m+"个数据,平均值为"+sum/m);
}
}