小编mak*_*er1的帖子

'<='无效的字符常量

您好我在'<='上收到一个错误,它是一个无效的字符常量?有什么想法吗?

switch (ch)
    {   
        case '+' : res = op1+op2;break;   
        case '-' : res = op1-op2;break;  
        case '*' : res = op1*op2;break;
        case '/' : if (op2 != 0)
                    res = op1/op2;
                   else 
                   System.out.println("Division by zero error in"+
                   " PostfixEvaluator.calculate().");
                   break;  
        case '%' : if (op2 != 0)
                    res = op1%op2;
                   else 
                   System.out.println("Division by zero error in"+
                   " PostfixEvaluator.calculate().");
                   break;  
        /**
         * Alterations begin here
         */
        case '<' : if(op1 < op2)
                    res = 1;
                   else
                    res = 0;
                   break; …
Run Code Online (Sandbox Code Playgroud)

java invalid-characters switch-statement

2
推荐指数
1
解决办法
3780
查看次数

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

这是我的问题.我从此方法中删除了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 …
Run Code Online (Sandbox Code Playgroud)

java compiler-errors switch-statement

0
推荐指数
1
解决办法
636
查看次数