public static void main(String[] args) throws IOException {
//下面这个正则会认为 ’-‘和'+'为数字
Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String[] split = br.readLine().split("\\s+");
double res = 0.0;
int x = 0;
for (int i = 0; i < split.length; i++) {
//关于123.这样的数字pat认为合法
if (split[i].contains(".")&&split[i].split("\\.").length==1){
split[i] = split[i].split("\\.")[0];
}
if (!pattern.matcher(split[i]).matches() || split[i].equals("-") || split[i].equals("+")) {
System.out.println("ERROR: " + split[i] + " is not a legal number");
} else {
if (Double.parseDouble(split[i]) > 1000.0 || Double.parseDouble(split[i]) < -1000.0) {
System.out.println("ERROR: " + split[i] + " is not a legal number");
} else {
if (/*精度问题————有小数点则判断小数点后是否小于等于2*/ split[i].contains(".")) {
if (split[i].split("\\.")[1].length() > 2) {
System.out.println("ERROR: " + split[i] + " is not a legal number");
} else {
x++;
res += Double.parseDouble(split[i]);
}
}else {
x++;
res += Double.parseDouble(split[i]);
}
}
}
}
DecimalFormat df=new DecimalFormat("0.00");
if (x != 0) {
if (x == 1){
res /= x;
System.out.println("The average of " + x + " number is " + df.format(res));
}else {
res /= x;
System.out.println("The average of " + x + " numbers is " + df.format(res));
}
} else {
System.out.println("The average of 0 numbers is Undefined");
}
}
测试点0、1是输入案例
测试点2是K为1的情况
测试点3是123.的情况(pat认为这个数字合法)