题解 | #记负均正II#注意正数为0的情况,格式化输出"%.1f\n"

记负均正II

http://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int temp = 0;
        int countN = 0;
        int countP = 0;
        double sum = 0.0;
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            temp = in.nextInt(); 
            if(temp<0){
                countN++;
            }else if(temp>0){
                countP++;
                sum += temp;
            }
        }
        System.out.println(countN);
        if(countP==0){
            System.out.printf("0.0");
        }else{
            System.out.printf("%.1f\n",sum/countP);
        }
    }
}
全部评论
在idea上你这些代码根本就运行不了
点赞
送花
回复
分享
发布于 2023-05-13 11:58 广东
统计非负数的平均值,你这里只统计了正数的平均值
点赞
送花
回复
分享
发布于 2023-08-10 11:53 江苏
滴滴
校招火热招聘中
官网直投

相关推荐

10 1 评论
分享
牛客网
牛客企业服务