菜鸟的博客

纵有疾风起,人生不言弃。

导航

2023.7.8

 1 import java.util.Scanner;
 2 
 3 public class test
 4 {
 5      public static void  main(String[] args)
 6      {
 7          int i = 0;
 8          float f = 0.0f;
 9 
10          Scanner scanner = new Scanner(System.in);
11 
12          System.out.println("请输入整数:");
13          if(scanner.hasNextInt())
14          {
15              i = scanner.nextInt();
16              System.out.println("整数数据为:"+i);
17          }
18          else
19          {
20              System.out.print("输入的数据不是整数数据!");
21          }
22          //////////////////////////////////////////////////
23          System.out.println("请输入小数:");
24          if(scanner.hasNextFloat())
25          {
26              f = scanner.nextFloat();
27              System.out.println("小数数据为:"+f);
28          }
29          else
30          {
31              System.out.println("输入的数据不是小数数据!");
32          }
33 
34          scanner.close();
35      }
36 }
 1 import java.util.Scanner;
 2 
 3 public class test
 4 {
 5      public static void  main(String[] args)
 6      {
 7          double sum = 0;//用于记录输入的总和
 8          double num = 0;//用于记录输入的个数
 9 
10          Scanner scanner = new Scanner(System.in);
11          while(scanner.hasNextDouble())
12          {
13              double x = scanner.nextDouble();
14              num++;
15              sum = sum+x;
16          }
17          System.out.println(num+"个数据的总和为:"+sum);
18          System.out.println(num+"个数据的平均值为:"+sum/num);
19          scanner.close();
20      }
21 }

 

posted on 2023-07-08 13:31  hhmzd233  阅读(6)  评论(0编辑  收藏  举报