删除了Switch语句以解决错误并且错误仍然存​​在.

mak*_*er1 0 java compiler-errors switch-statement

这是我的问题.我从此方法中删除了switch语句,因为我必须更改它以适应多个变量,而switch语句只允许char常量.在没有java版本7的情况下,无法将字符串用于变量.所以我所做的就是改为通常的if/else语句.但是在尝试运行程序时,我仍然在switch语句中收到错误,如下所示:

有任何想法吗?如果我希望包含测试人员的代码,我可以问一下.

java.lang.Error:未解决的编译问题:无法为源级别低于1.7的值打开String类型的值.只允许使用可转换的int值或枚举变量

在Project2.PostfixEvaluator.calculate(PostfixEvaluator.java:124)位于Project2.PostfixEvaluator.eval(PostfixEvaluator.java:71)的Project2.ExpressionEvaluator.evaluate(ExpressionEvaluator.java:29),位于Project2.ExpressionEvaluatorTester.main(ExpressionEvaluatorTester.java: 32)

public Token calculate(Token opr, Token opd1, Token opd2)
    {
        // Get the first String from opr, it is the operator: +, -, ...
        String oper = opr.getBody();

        System.out.println(opr);

        //Get the two operands by converting from String to int
        int op1 = Integer.parseInt(opd1.getBody());
        int op2 = Integer.parseInt(opd2.getBody());

        //Default return value, in case an error occurs
        int res = 0;

        /**
         * Alterations begin here
         * Performs operation and sets value for res
         */

           if(oper.equals("+")) 
           {
               res = op1+op2;
           }
           else if(oper.equals("-"))
           {
               res = op1-op2;  
           }
           else if(oper.equals("*")){
               res = op1*op2;
           }
           else if(oper.equals("/") && op2 != 0){
               res = op1/op2;
           }
           else if(oper.equals("/") && op2 == 0){
               System.out.println("Division by zero error in"+
                 " PostfixEvaluator.calculate().");
           }
           else if(oper.equals("%") && op2 != 0){
                res = op1%op2;
           }
           else if(oper.equals("%") && op2 == 0){
                System.out.println("Division by zero error in"+
                 " PostfixEvaluator.calculate().");
           }
           else if(oper.equals("<") && (op1 < op2)){
                res = 1;
           }
           else if(oper.equals("<") && (op1 >= op2)){
                res = 0;
           }
           else if(oper.equals("<=") && (op1 <= op2)){
                res = 1;
           }
           else if(oper.equals("<=") && (op1 > op2)){
                res = 0;
           }
           else if(oper.equals(">") && (op1 > op2)){
                res = 1;
           }
           else if(oper.equals(">") && (op1 <= op2)){
                res = 0;
           }
           else if(oper.equals(">=") && (op1 >= op2)){
                res = 1;
           }
           else if(oper.equals(">=") && (op1 < op2)){
                res = 0;
           }
           else if(oper.equals("==") && (op1 == op2)){
                res = 1;
           }
           else if(oper.equals("==") && (op1 != op2)){
                res = 0;
           }
           else if(oper.equals("!=") && (op1 != op2)){
                res = 1;
           }
           else if(oper.equals("!=") && (op1 == op2)){
                res = 0;
           }
           else if(oper.equals("||") && true){
                res = 1;
           }
           else if(oper.equals("&&")&& true){
                res = 1;
           }
           else
               res = 0;





        //Convert res into a Token and return it.
        return new Token(""+res);

}
Run Code Online (Sandbox Code Playgroud)

Mar*_*nik 6

由于这不是编译器错误,而是运行时错误,这意味着您更改了源代码,但无法重新编译它,或者至少无法运行重新编译的代码.

该错误肯定与switch源代码中的语句问题有关,但您已从源代码文件中删除了该错误.因此,您必须加载过时版本的.class文件.检查是否已激活" 自动构建"选项和/或执行完整项目清理+重建.