java—制作一个字符串行计算器

8iwquhpp  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(250)

现在我正在做一个单字符串计算器,学习正则表达式,我有一些问题。
对于2个数字,例如:2+2,输出将是4,但是如果我尝试输入3个数字:2+2+2,输出将是4,忽略第三个数字,但是我将通过operands.length。
另一个,如果我尝试将第一个数字设为负数,比如:-2+2,它会给我一个错误:在线程“main”java.lang.numberformatexception中异常:对于输入字符串:“”。
string[]operators=input.split(“\s*-?\w+\s*”);不起作用,但为什么?

Scanner scanner = new Scanner(System.in);

        while (true) {
            String input = scanner.nextLine();

            boolean ifExit = false;
            boolean ifHelp = false;
            boolean ifEmpty = false;
            boolean ifOneNumber = false;

            String[] operators = input.split("\\s*\\w+\\s*");
            String[] operands = input.split("\\s*[+-]+\\s*");

            int result = 0;

            if (operands[0].matches("/exit")) {
                ifExit = true;
                System.out.println("Bye!");
                break;
            } else if (operands[0].matches("/help")) {
                ifHelp = true;
                System.out.println("The program calculates the sum of numbers");
            } else if (operands[0].matches("")) {
                ifEmpty = true;
            } else if (operands[0].matches("[0-9]+") && operands.length == 1) {
                ifOneNumber = true;
                result += Integer.parseInt(operands[0]);
                System.out.println(result);
            }

            for (int i = 1; i < operands.length; i++) {
                result = Integer.parseInt(operands[0]);

                if (operators[i].matches("[+]")) {
                    allBooleansAreFalse(ifHelp, ifEmpty, ifOneNumber);
                    result += Integer.parseInt(operands[i]);
                } else if (operators[i].matches("[-]")){
                    allBooleansAreFalse(ifHelp, ifEmpty, ifOneNumber);
                    result -= Integer.parseInt(operands[i]);
                } else if (operators[i].matches("[+]{2,1000}")) {
                    allBooleansAreFalse(ifHelp, ifEmpty, ifOneNumber);
                    result += Integer.parseInt(operands[i]);
                } else if (operators[i].matches("[-]{2}")) {
                    allBooleansAreFalse(ifHelp, ifEmpty, ifOneNumber);
                    result += Integer.parseInt(operands[i]);
                } else if (operators[i].matches("[-]{3}")) {
                    allBooleansAreFalse(ifHelp, ifEmpty, ifOneNumber);
                    result -= Integer.parseInt(operands[i]);
                }
            }
            if (ifExit == true) {
                break;
            } else if (ifExit == false && ifHelp == false && ifEmpty == false && ifOneNumber == false) {
                System.out.println(result);
            }
        }

    }

    public static void allBooleansAreFalse(boolean ifHelp, boolean ifEmpty, boolean ifOneNumber) {
        ifHelp = false;
        ifEmpty = false;
        ifOneNumber = false;
    }
krugob8w

krugob8w1#

我通过在for循环之前添加以下代码修复了第一个问题:

else if (operands[0].matches("[0-9]")) {
                result = Integer.parseInt(operands[0]);
            }

在for循环中删除以下代码:

result = Integer.parseInt(operands[0]);

现在我需要修正负数的问题,如果第一个负数是-2+2,它会给我一个错误,有人吗?

相关问题