标签:进阶 oat system ace 个数 执行 style bsp print
1 package com.pingfan.scanner; 2 3 import java.util.Scanner; 4 5 public class Demo3 { 6 public static void main(String[] args) { 7 Scanner scanner = new Scanner(System.in); 8 9 //从键盘接收数据 10 int i = 0; 11 float f = 0.0f; 12 13 System.out.println("请输入整数:"); 14 15 //如果...那么 16 if(scanner.hasNextInt()){ 17 i = scanner.nextInt(); 18 System.out.println("整数数据:" + i); 19 } 20 else{ 21 System.out.println("输入的不是整数数据!"); 22 } 23 System.out.println("请输入小数:"); 24 //如果...那么 25 if(scanner.hasNextFloat()){ 26 f = scanner.nextFloat(); 27 System.out.println("小数数据:" + f); 28 } 29 else { 30 System.out.println("输入的不是小数数据!"); 31 } 32 33 34 scanner.close(); 35 } 36 }
1 package com.pingfan.scanner; 2 3 import com.sun.org.apache.xerces.internal.impl.xs.SchemaNamespaceSupport; 4 5 import java.util.Scanner; 6 import java.util.concurrent.CancellationException; 7 8 public class Demo4 { 9 public static void main(String[] args) { 10 //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果: 11 Scanner scanner = new Scanner(System.in); 12 13 //和 14 double sum = 0; 15 //计算输入了多少个数字 16 int m = 0; 17 18 //通过循环判断是否还有输入,并在里面对每一次进行求和和统计 19 while (scanner.hasNextDouble()){ 20 double x = scanner.nextDouble(); 21 m = m + 1;//m++ 22 sum = sum + x; 23 System.out.println("你输入了第" + m + "个数据,然后当前结果sum=" + sum); 24 } 25 System.out.println(m + "个数的和为" + sum); 26 System.out.println(m + "个数的平均值是" + (sum / m)); 27 scanner.close(); 28 } 29 }
标签:进阶 oat system ace 个数 执行 style bsp print
原文地址:https://www.cnblogs.com/HeartlessHero/p/14552783.html