publicclassDemo04{publicstaticvoidmain(String[] args){//
Scanner scanner =newScanner(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();}}
publicclassDemo05{publicstaticvoidmain(String[] args){//我们可以输入多个数字,并求其总和与平均数,每输入一个数字回车确认,通过输入非数字来结束输入并输出执行结果:
Scanner scanner =newScanner(System.in);//和double sum =0;//计算输入了多少个数字int m =0;
System.out.println("请输入数据:");//通过循环判断是否还有输入,并在里面对每一次进行求和统计while(scanner.hasNextDouble()){double x = scanner.nextDouble();
m = m +1;
sum = sum + x;
System.out.println("你输入了第"+ m +"个数据,当前结果sum = "+ sum);}
System.out.println(m +"个数的和为"+ sum);
System.out.println(m +"个数的平均值为"+(sum / m));
scanner.close();}}
public class Demo01 { public static void main(String[] args) { //创建一个扫描器对象,用于接收键盘数据 Scanner scanner = new Scanner(System.in); System.out.println("使用next方式接受:"); //判断用户有没有输入字符串 if (scanner.hasNext()){