Type mismatch exception

折月煮酒 提交于 2019-12-25 06:37:17

问题


I am developing game, Initially I was using boolean while decalring arrays, latter it revealed that instead of using boolean I should use int in order to store state of game, When I replaced boolean with int my if statement shows type mismatch exception and The operator && is undefined for the argument type(s) boolean, int. Here is my if statement code.

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);
        }
    }

回答1:


That is because you cannot use the AND && and OR || operators with integers, so you may want to re-define the condition:

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

I can't give you a "real " fix, because it depends on what you are trying to do. You can try this, but may not work as you expect:

if (left && up)



回答2:


Yes, when left and up are used as int variable then following if condition will give type mismatch exception

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

As the result of left && up will be an integer and then you are performing logical AND between an int and boolean variable, so it will give type mismatch exception.

You should use it as following way -

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

Where intValue is valid out value in your case and now (left && up).equals(intValue) will give a boolean value which can easily use with other boolean value dots[x - 1][y - 1]

See logical operations on int variables -

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



回答3:


You are trying to use an int type in a conditional statement that will evaluate boolean expressions, leading to a type mismatch. Unlike other languages, in Java 0 does not correspond to false too (and variables of boolean type can only be true or false, not 0 or 1). You will have to set up an expression in the statement that will give out a boolean result.

For example, you can do:

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

instead of:

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

Now, if you are using a specific number in your dots array that is the unwanted situation to check with, you check with that number instead of 0.

The same rules occur if there are multiple expressions combined with && and/or || operators, in your conditional statements.



来源:https://stackoverflow.com/questions/22979939/type-mismatch-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!