题解 | #求平均数#

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    //write your code here......
    //初始化整数和
    float sum=0;
    //统计个数,先初始化
    int i=0;
    while (true) {
        int PotiveInt=scan.nextInt();
        //输入后先判断是否为负数
        if (PotiveInt<0) break;
        i++;//满足条件,个数+1
        sum+=PotiveInt;//满足条件,计算总和
    }
    float average=sum/i;
    System.out.println(String.format("%.2f",average));//格式化输出

    //输出格式为:System.out.println(String.format("%.2f",avg));
    

}

}

全部评论

相关推荐

头像
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务