zl程序教程

您现在的位置是:首页 >  后端

当前栏目

pat 1108 Finding Average Java

JAVA PAT average
2023-09-14 09:13:21 时间

pat 1108 Finding Average Java

0. 题目简介

题目比较水,这里不再介绍了。

1. 代码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sn = new Scanner(System.in);
        double[] cal = new double[100];
        List<String> invalidStr = new ArrayList<String>();

        int count = 0;
        int number ;
        number = Integer.parseInt(sn.nextLine());

        String[] line = sn.nextLine().trim().split(" ");
        int curIndex = 0;
        //traverse the next input
        while (curIndex < line.length ) {
            String temp = line[curIndex];
            if (isNormal(temp)) {
                cal[count++] = Double.parseDouble(temp);
            } else {
                invalidStr.add(temp);
            }
            curIndex++;
        }
        for (String str : invalidStr) {
            System.out.println("ERROR: "+str+" is not a legal number");
        }
        double sum = 0;
        for(int i = 0;i< cal.length;i++) {
            sum += cal[i];
        }
        double res;
        if (count == 1) {
            res = sum/count;
            System.out.printf("The average of %d number is %.2f", count ,res);
        } else if (count > 1) {
            res = sum/count;
            System.out.printf("The average of %d numbers is %.2f", count ,res);
        } else {
            System.out.println("The average of 0 numbers is Undefined");
        }
    }

    // 判断输入的值是否合法
    public static boolean isNormal(String str) {
        //if can return any double, return true
        try {
            if (Double.parseDouble(str) <= 1000 && Double.parseDouble(str) >= -1000) {
                //判断小数点后面的位数
                return pointLegal(str);
            }
            else    return false;
        } catch (Exception e) { //输入的值不合法  =>  返回false
            return false;
        }
    }

    public static boolean pointLegal(String str) {
        char [] chars = str.toCharArray();
        int indexOfPoint ;
        int length = str.length();

        if (str.contains(".")) {//如果包含"."号,则计算点号的位置
            indexOfPoint = str.indexOf(".");
            if (indexOfPoint == 0) {//过滤 .23 这种数据
                return false;
            }
            if (indexOfPoint > 0 && chars[indexOfPoint - 1] == '-') {//过滤 -.23 这种数据
                return false;
            }
            if (length - indexOfPoint > 3)
                return false;
            else return true;
        }
        return true;
    }
}

2.测试用例

2.1
3
aaa -999 -.23
2.2
3
1 2b 1000
2.3
4
1 2b 1000 -1000
2.4
1
1
The average of 1 number is 1.00

注意这里当只有一数参与了计算的时候,输出值的时候是number 而不是 numbers。但是在没有参数参与计算的时候,仍然输出numbers【我也不知道姥姥是怎么想的。。。+_+】

3. 其它坑点

  • Double.parseInt() 有一定的容错性 -.23自动翻译成 -0.23
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double value = Double.parseDouble(sc.next());
        System.out.println(value);
    }
}
  • 输入参数
    当我们输入-.23时,在使用Double.parseDouble(sc.next())输出时,发现其是可以解析的。【因为java包对其进行了容错处理啊】 这种容错处理就会导致判断错误,因为-.23是不合理的输入,但是java却没有办法将其过滤掉。所以正确的方式就是避免使用java带的包,而自己手动写解析过程
  • 执行结果
    在这里插入图片描述