【问题标题】:Type mismatch exception类型不匹配异常
【发布时间】:2014-05-23 16:21:37
【问题描述】:

我正在开发游戏,最初我在标记数组时使用布尔值,后来它表明我应该使用 int 而不是使用 boolean 来存储游戏状态, 当我用 int 替换 boolean 时,我的 if 语句显示 type mismatch exceptionThe operator && is undefined for the argument type(s) boolean, int。这是我的 if 语句代码。

int [][] dots

    protected void onDraw(Canvas canvas) 
    {
        super.onDraw(canvas);
        canvas.drawPaint(pBack);

        for (int y = 0; y < numRows; y++)
        {
            canvas.drawLine(xStep, yCoords[y], numColumns * xStep, yCoords[y], pDot);

            for (int x = 0; x < numColumns; x++)
            {
                if (y == 0)
                {
                    canvas.drawLine(xCoords[x], yStep, xCoords[x], numRows * yStep, pDot);
                }

                if (dots[x][y])
                {
                    boolean left = x > 0 && dots[x - 1][y];
                    boolean up = y > 0 && dots[x][y - 1];

                    if (left)
                    {
                        canvas.drawLine(xCoords[x], yCoords[y], xCoords[x - 1], yCoords[y], pLine);
                    }

                    if (up)
                    {
                        canvas.drawLine(xCoords[x], yCoords[y], xCoords[x], yCoords[y - 1], pLine);
                    }

                    if (left && up && dots[x - 1][y - 1])
                    {
                        canvas.drawCircle(xCoords[x] - xStep / 2, yCoords[y] - yStep / 2, 10, pLine);
                    }
                }
            }
        }
       for (int y = 0; y < numRows; y++)
        {
            for (int x = 0; x < numColumns; x++)
            {
                canvas.drawCircle(xCoords[x], yCoords[y], 20, pDot);                    
                if (dots[x][y])
                {
                    canvas.drawCircle(xCoords[x], yCoords[y], 15, pLine);                       
                }                                       
            }
        }
        if (firstDotX != -1)
        {
            canvas.drawCircle(xCoords[firstDotX], yCoords[firstDotY], 25, pSelect);
        }
    }

【问题讨论】:

  • 嗯。 int 不是 boolean。不能像使用布尔值那样使用整数值。
  • 我没有看到ints,这是您的旧代码还是新代码?
  • @TimCastelijns 我猜,dots 变量(或者它是一个字段?)之前是 boolean[][],现在是 int[][]。而这——当然——是行不通的。他描述的错误很可能出现在声明和初始化布尔变量 left 和 up 的行中。
  • 当然,我们可以猜测。但我们不知道,这就是我问的原因:-)
  • 那么我建议你暂时彻底摆脱dots,专注于边和框,以及你想如何跟踪它们的状态。此外,您将需要更改onTouchEvent() 方法以找到最近的一侧,而不是最近的点,但这是一个简单的修改。如果边是由玩家“拥有”的,那么您将需要以类似于框的方式跟踪它们,即使用ints。

标签: java if-statement int boolean type-mismatch


【解决方案1】:

这是因为您不能对整数使用 AND &amp;&amp; 和 OR || 运算符,因此您可能需要重新定义条件:

if (left && up && dots[x - 1][y - 1])
                  ------------------
                  this is an integer

我不能给你一个“真正的”修复,因为这取决于你想要做什么。你可以试试这个,但可能不会像你期望的那样工作:

if (left && up)

【讨论】:

  • @UmarFarooq dots 是什么?我的意思是,它在程序中代表什么?
  • dot 是一个数组。 int [][] 点
  • 它将drawcalls存储在“circles”上。
  • 你能举个例子吗?
【解决方案2】:

是的,当 left 和 up 用作 int 变量时,遵循 if 条件将给出 类型不匹配异常

if (left && up && dots[x - 1][y - 1])

由于 left && up 的结果是一个整数,然后你在一个 int 和 boolean 变量之间执行逻辑与,所以它会给出类型不匹配的异常。

你应该按照以下方式使用它-

if (((left && up).equals(intValue) && dots[x - 1][y - 1])

在您的情况下 intValue 是有效的值,现在 (left && up).equals(intValue) 将给出一个布尔值,可以轻松地与其他布尔值点 [x - 1][y - 1] 一起使用p>

查看 int 变量的逻辑运算 -

2 | 1 = 3 and 4 | 1 = 5. 

【讨论】:

    【解决方案3】:

    您试图在将评估boolean 表达式的条件语句中使用 int 类型,从而导致类型不匹配。与其他语言不同,Java 中0 也不对应false(并且boolean 类型的变量只能是truefalse,而不是01)。您必须在语句中设置一个表达式,该表达式将给出一个布尔结果。

    例如,你可以这样做:

    if(dots[x][y] == 0){....}
    

    代替:

    if(dots[x][y]){....}
    

    现在,如果您在点数组中使用特定数字,这是不希望检查的情况,请使用该数字而不是 0 进行检查。

    如果条件语句中有多个表达式与&amp;&amp; 和/或|| 运算符结合使用,则会出现相同的规则。

    【讨论】:

    • 我已经看到了,但我无法实现它。 stackoverflow.com/questions/3793650/…
    • 您不能将所有布尔值转换为整数。关键是相反,因为条件语句(if、while 等)使用布尔值。我给出的答案是如何使用 int 来获得布尔结果。反之在这里将毫无用处。
    猜你喜欢
    • 2012-07-24
    • 2013-09-26
    • 1970-01-01
    • 2012-09-02
    • 2013-08-25
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多