Scanner进阶使用
整数:hasNextInt()——nextInt()
小数:hasNextFloat()——nextFloat()
if:判断语句
while:循环语句
练习:
循环输入,求和与平均数,回车确认,非数字结束指令并输出结果。
Scanner scanner = new Scanner(System.in);//开始
double sum = 0;
int m = 0;
System.out.println("请输入:")
11
while(scanner.hasNextDouble()){
double x = scanner.nextDouble();
m = m + 1;
sum = sum + m;
System.out.println("当前个数为:"+m+"和为:"+sum);
}
System.out.println("当前个数为:"+m+"和为:"+sum);
System.out.println("平均值为:"+sum/m);
scanner.cloth;//结束
标签:01,Java,scanner,sum,System,println,out,Scanner
From: https://www.cnblogs.com/sssm0199/p/17983405