题解 | #记负均正II#sum必须是double类型的

记负均正II

https://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 negs = 0;

        double sum = 0;
        int posNum = 0;
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int x = in.nextInt();
            if(x > 0){
                posNum++;
                sum+=x;
            }else if(x<0){
                negs++;
            }
        }
        System.out.printf("%d\n",negs);
        if(posNum!=0)
            System.out.printf("%.1f",sum/posNum);
        else    
            System.out.printf("0.0");

    }
}

全部评论

相关推荐

2025-12-30 16:42
同济大学 C++
仁狂躁使者:哎呀,不用担心,我当时配环境配了两天,项目捋不清就问问导师能不能用ai,慢慢就清了,会好起来的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务