从输入任意个整型数,统计其中的负数个数并求所有非负数的平均值,结果保留一位小数,如果没有非负数,则平均值为0
记负均正II
http://www.nowcoder.com/questionTerminal/64f6f222499c4c94b338e588592b6a62
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args)throws IOException {
//题目有缺陷,实际是单行输入,如果按照多行无法通过
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] strings = reader.readLine().split(" ");
int countN = 0;
int countP = 0;
int sumN = 0;
for (int i = 0; i < strings.length; i++) {
try {//考虑输入有别的字符
int temp = Integer.parseInt(strings[i]);
if (temp < 0) {
countN++;
}
if (temp > 0) {
sumN += temp;
countP++;
}
} catch (NumberFormatException e) {
}
}
System.out.println(countN);
//处理正数总数为零的情况 除数为0
if (countP > 0) {
//四舍五入 用double类型四舍五入函数DecimalFormat 之后小数四舍五入值不对
System.out.print(Math.round(sumN * 10.0 / countP) / 10.0);
} else {
System.out.print("0.0");
}
}
}
