Java 整数和布尔错误

Java 整数和布尔错误,java,boolean,Java,Boolean,我有一个方法,它生成一个错误,一个int应该是布尔值,但是当我把它转换成布尔值时,它说的是相同的错误,但是int和布尔值相反。这是我的密码: private void compileDeclaration(boolean isGlobal) { if (equals(theToken, "int")) { accept("int"); String ident = theToken; if (!isIden

我有一个方法,它生成一个错误,一个int应该是布尔值,但是当我把它转换成布尔值时,它说的是相同的错误,但是int和布尔值相反。这是我的密码:

private void compileDeclaration(boolean isGlobal) {
         if (equals(theToken, "int")) {
            accept("int");
            String ident = theToken;
            if (!isIdent(theToken)) t.error("expected identifier, got " + theToken);
            else if (isGlobal){
                symTable.allocVar(ident, isGlobal);
            }

            if (!isGlobal) cs.emit(Machine.ALLOC, symTable.stackFrameSize());
            //dprint("declaring int " + ident);
            theToken = t.token();
            accept (";");
        } else if (equals (theToken, "final")) {
            accept("final");
            accept("int");
            String ident = theToken;
            if (!isIdent(theToken)) t.error("expected identifier, got " + theToken);
            theToken = t.token();
            accept("=");
            int numvalue = new Integer(theToken).intValue();
            if (!isNumber(theToken)) t.error("expected number, got " + theToken);
            else if (numvalue = 0) { **//This is where it highlights my error**
                symTable.allocConst(ident, numvalue);
            }

任何帮助都将不胜感激。

很可能您在两个不同的位置调用它,一个是整数,一个是布尔值

该行或
symTable.allocVar()
需要整数。

该行

else if (numvalue = 0) { **//This is where it highlights my error**
缺少一个等于符号,即

else if (numvalue == 0) { **//This is where it highlights my error**

为了解释这个问题:“numvalue=0”要求numvalue是一个int(或long),这样就可以将0赋值给它,因此“应该是int”。但是,if语句需要一个布尔表达式,并且赋值不是布尔表达式,因此“bool expected”——两个不同的错误,由缺少=。如果这是C/C++,你会有很多乐趣:-我同意C++会给你一个很好的运行!谢谢你的解释。很好,你展示了代码,你只需要展示错误信息,这样会更容易发现。我认为颜飞利浦的答案是正确的,你应该让他被接受。