如何对字符串数组列表中的两个数字进行数学表达式

我试图从数组列表中读取符号,然后对其前后的两个数字进行表达式(例如, i-1 * i+1) 。然而,生成的结果总是 5、6 或 12!我真的找不到代码中的问题所在。


我使用 arraylist 是因为它比数组更动态,从某种意义上说,我可以删除元素而不用关心“数组”的长度。


    String replaceStr = replacer.replace("+", " + ");

我正在尝试添加空格以将整数作为一个元素,而不是使用 charAt,这将不允许我将一个多于一位的数字带入等式。


    Double Computing(String equation) {

    double result = 0.0;

    double multiplResult = 1.0; //


    String replacer = equation;


    String replaceStr = replacer.replace("+", " + ");

    String replaceStr1 = replaceStr.replace("x", " x ");

    String replaceStr2 = replaceStr1.replace("-", " - ");

    String replaceStr3 = replaceStr2.replace("/", " / ");

    String[] items = replaceStr3.split("\\s*");


    ArrayList<String> list = new ArrayList<String>((Arrays.asList(items)));





   for (int i = 0; i < list.size(); i++ ) {

       String NewNum;


       if (list.get(i) == "x" || list.get(i) == "/"){


           if(list.get(i) == "x") {

               NewNum = String.valueOf(Double.parseDouble(list.get(i - 1)) * Double.parseDouble(list.get(i + 1)));

               list.set(i, NewNum);

               list.remove(i-1);

               list.remove(i+1);

           }

           else if (list.get(i) == "/"){

               NewNum = String.valueOf(Double.parseDouble(list.get(i - 1)) / Double.parseDouble(list.get(i + 1)));

               list.set(i, NewNum);

               list.remove(i-1);

               list.remove(i+1);

           }

           multiplResult *= Double.parseDouble(list.get(0));

       }

       if (list.get(i) == "+" || list.get(i) == "-"){


           if(list.get(i) == "+") {

               NewNum = String.valueOf(Double.parseDouble(list.get(i - 1)) + Double.parseDouble(list.get(i + 1)));

               list.set(i, NewNum);

               list.remove(i-1);

               list.remove(i+1);

           }

           }

           multiplResult *= Double.parseDouble(list.get(0));

       }

       result += multiplResult;

    }

return result;

}


慕的地10843
浏览 144回答 2
2回答

繁星coding

这是一个简单的评估器:public class Evaluator {&nbsp; &nbsp; private final char[] chars;&nbsp; &nbsp; private int pos;&nbsp; &nbsp; public static double evaluate(String expr) {&nbsp; &nbsp; &nbsp; &nbsp; return new Evaluator(expr).expression();&nbsp; &nbsp; }&nbsp; &nbsp; private Evaluator(String expr) {&nbsp; &nbsp; &nbsp; &nbsp; chars = expr.toCharArray();&nbsp; &nbsp; }&nbsp; &nbsp; private double expression() {&nbsp; &nbsp; &nbsp; &nbsp; double value = term();&nbsp; &nbsp; &nbsp; &nbsp; char op;&nbsp; &nbsp; &nbsp; &nbsp; while (skipSpaces() && ((op = chars[pos]) == '+' || op == '-')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double other = term();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (op) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case '+':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value += other;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case '-':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value -= other;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; }&nbsp; &nbsp; private double term() {&nbsp; &nbsp; &nbsp; &nbsp; double value = factor();&nbsp; &nbsp; &nbsp; &nbsp; char op;&nbsp; &nbsp; &nbsp; &nbsp; while (skipSpaces() && ((op = chars[pos]) == 'x' || op == '/')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double other = factor();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (op) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'x':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value *= other;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case '/':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value /= other;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; }&nbsp; &nbsp; private double factor() {&nbsp; &nbsp; &nbsp; &nbsp; if (skipSpaces() && chars[pos] == '(') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double result = expression();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (skipSpaces() && chars[pos] == ')') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return number();&nbsp; &nbsp; }&nbsp; &nbsp; private double number() {&nbsp; &nbsp; &nbsp; &nbsp; if (!skipSpaces()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int start = pos;&nbsp; &nbsp; &nbsp; &nbsp; if (chars[pos] == '+' || chars[pos] == '-') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pos;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (pos >= chars.length || !Character.isDigit(chars[pos])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pos;&nbsp; &nbsp; &nbsp; &nbsp; } while (pos < chars.length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && (Character.isDigit(chars[pos]) || chars[pos] == '.'));&nbsp; &nbsp; &nbsp; &nbsp; return Double.parseDouble(new String(chars, start, pos-start));&nbsp; &nbsp; }&nbsp; &nbsp; private boolean skipSpaces() {&nbsp; &nbsp; &nbsp; &nbsp; while (pos < chars.length && Character.isWhitespace(chars[pos])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++pos;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return pos < chars.length;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java