题解 | #求平均数#

求平均数

https://www.nowcoder.com/practice/41e59cee1221424bb9291435aae79ae9

import java.util.Scanner;

public class Main {

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

        //write your code here......
        float avg, sum = 0;
        int num = 0;
        while (scan.hasNextInt() ) {
            int nextInt = scan.nextInt();
            num += nextInt > 0 ? 1 : 0;
            sum += nextInt > 0 ? nextInt : 0;
        }
        avg = sum / num;

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

全部评论

相关推荐

评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务