差点没给我整郁闷

记负均正II

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

本地用scanner.hasNextInt时,一直等待输入,就是不跳出循环,在牛客提交又行。。。。。



import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int negative = 0;
        // 非负数累计值
        BigDecimal noNegativeCount = new BigDecimal("0");
        // 非负数的个数值
        BigDecimal noNegative = new BigDecimal("0");

        while (scanner.hasNextInt()) {
            String nextLine = scanner.nextLine();
            BigDecimal bigDecimal = new BigDecimal(nextLine);
            if (bigDecimal.compareTo(new BigDecimal("0")) < 0) {
                negative++;
            } else {
                // 总值
                noNegativeCount = noNegativeCount.add(new BigDecimal(nextLine));
                // 数量
                noNegative = noNegative.add(new BigDecimal("1"));
            }
        }

        System.out.println(negative);
        if (noNegativeCount.compareTo(new BigDecimal("0")) == 0) {
            System.out.println("0.0");
        } else {
            // 保留一位小数输出
            System.out.println(noNegativeCount.divide(noNegative, 1, RoundingMode.HALF_UP));
        }

    }
}

全部评论

相关推荐

下北澤大天使:你是我见过最美的牛客女孩😍
点赞 评论 收藏
分享
Beeee0927:是缅甸园区吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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