Java - 值突然改变并且打印行语句运行两次?

时间:2017-09-23 07:45:30

标签: java

我有以下代码块:

    } else {
        System.out.println("call");
        int left = solveExpression(leftExpression.toString(), enviornment);
        int right = solveExpression(rightExpression.toString(), enviornment);
        System.out.println(left + "  " + right);
        return left % right;
    }

代码的输出完全如下:

呼叫

10 2

12 0

Exception in thread "main" java.lang.ArithmeticException: / by zero
at PrefixSolver.getOperatorExpression(PrefixSolver.java:245)
at PrefixSolver.solveExpression(PrefixSolver.java:83)
at PrefixSolver.parseDefinition(PrefixSolver.java:168)
at PrefixSolver.parseEnviornment(PrefixSolver.java:122)
at PrefixSolver.solveExpression(PrefixSolver.java:55)
at PrefixSolver.parseDefinition(PrefixSolver.java:168)
at PrefixSolver.parseEnviornment(PrefixSolver.java:122)
at PrefixSolver.solveExpression(PrefixSolver.java:55)
at PrefixSolver.getOperatorExpression(PrefixSolver.java:232)
at PrefixSolver.solveExpression(PrefixSolver.java:83)
at PrefixSolver.parseDefinition(PrefixSolver.java:168)
at PrefixSolver.parseEnviornment(PrefixSolver.java:122)
at PrefixSolver.solveExpression(PrefixSolver.java:55)
at PrefixSolver.getOperatorExpression(PrefixSolver.java:232)
at PrefixSolver.solveExpression(PrefixSolver.java:83)
at PrefixSolver.solve(PrefixSolver.java:12)
at Prefix.main(Prefix.java:17)

我很困惑,甚至可能这样做。第一个println是正确的(10 2)但是它以某种方式改变了?只是为了看看是否以某种方式再次调用solveExpression我将println' s放入方法中并且不再被调用。价值观正在以某种方式改变..?

1 个答案:

答案 0 :(得分:0)

看起来像一个递归方法调用 - 正如亨利所说,去调试看看哪些调用以及传递了什么参数