【Java】用键盘输入若干数字,以非数字字符结束,计算这些数的和和平均值

package com.company;

import  java.util.*;
public class Main {
    public static void main(String arg[]) {
        Scanner input = new Scanner(System.in);
        double sum = 0;
        int m = 0;
        while (input.hasNextDouble()) {
            double x=input.nextDouble();
            m++;
            sum+=x;
        }
        System.out.printf("%d个数的和为%f\n",m,sum);
        System.out.printf("%d个数的平均值为%f\n",m,sum/m);
    }
}

 

posted @ 2021-03-26 22:22  木子欢儿  阅读(387)  评论(0编辑  收藏  举报