package com.wang.scanner;
import java.sql.SQLOutput;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i=0;
float f=0.0F;
System.out.println("请输入整数");
if(scanner.hasNextInt()){
i=scanner.nextInt();
System.out.println("整数:"+i);
}else{
System.out.println("不是整数:");
}
System.out.println("请输入小数");
if(scanner.hasNextFloat()){
f=scanner.nextFloat();
System.out.println("小数:"+f);
}else{
System.out.println("不是小数:");
}
scanner.close();
}
}
求和平均值
package com.wang.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; //m++
}
System.out.println(m+"个数的和是:"+sum);
System.out.println(m+"个数的平均值是:"+(sum/m));
scanner.close();
}
}
标签:13,进阶,scanner,sum,System,println,out,Scanner
From: https://www.cnblogs.com/PedroPascal/p/17093168.html