题解 | HJ105#记负均正II#
记负均正II
https://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int numOp = 0; int numNeg = 0; int sumOp = 0; while (scanner.hasNextInt()) { int nextInt = scanner.nextInt(); if (nextInt < 0) { numNeg++; } else { numOp++; sumOp += nextInt; } } //加入限制条件的判断 若正数为0 则输出0.0. if (numOp == 0) { System.out.println(0.0); } else { //输出一位小数 float res = (float) sumOp / numOp; String format = String.format("%.1f", res); System.out.println(numNeg); System.out.println(format); } } }