com.xu.scanner.Demo05

package com.xu.scanner;

import java.util.Scanner;

public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车
//确认,通过输入非数字来结束输入并输出执行结果;
Scanner scanner=new Scanner(System.in);
//和
double sum=0;
//计算输入了多少个数字;
int m=0;
System.out.println("请输入数据:");
//通过循环判断是否还有输入,并在里面对每一次进行求和统计
while (scanner.hasNextDouble()){
double x =scanner.nextDouble();

m=m+1;//可以写成m++
sum=sum+x;
System.out.println("你输入了第"+m+"个数据,然后当前结果sum:"+sum);
}
System.out.println(m+"个数的和为"+sum);
System.out.println(m+"个数的平均值是"+(sum/m));

scanner.close();

}
}

"D:\Program Files\Java\jdk1.8.0_281\bin\java.exe" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2021.1\lib\idea_rt.jar=64192:D:\Program Files\JetBrains\IntelliJ IDEA 2021.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_281\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_281\jre\lib\rt.jar;C:\Users\徐文昭\Desktop\JavaSE\out\production\基础语法" com.xu.scanner.Demo05
请输入数据:
123
你输入了第1个数据,然后当前结果sum:123.0
456
你输入了第2个数据,然后当前结果sum:579.0
789
你输入了第3个数据,然后当前结果sum:1368.0
m
3个数的和为1368.0
3个数的平均值是456.0

Process finished with exit code 0



上一篇:com.xu.operator.Demo05


下一篇:cmo.xu.base.Demo05