Why is this code giving an “Unreachable Statement” error?

筅森魡賤 提交于 2019-11-27 04:54:08

问题


This is my code and im getting an unreachable statement error on it but i do not know why.

public boolean Boardload(String[] args) throws Exception
{
    Robot robot = new Robot();
    Color color3 = new Color(114, 46, 33);
    Color color4 = new Color(180, 0, 0);

    {
        Rectangle rectangle = new Rectangle(0, 0, 1365, 770);

        {
            while(false)
            {
                BufferedImage image = robot.createScreenCapture(rectangle);
                search: for(int x = 0; x < rectangle.getWidth(); x++)
                {
                    for(int y = 0; y < rectangle.getHeight(); y++)
                    {
                        if(image.getRGB(x, y) == color3.getRGB())
                        {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
} 

the exact error is:

java:68: unreachable statement
         {
         ^

Help would be nice, this code is supposed to loop until the pixel is found.


回答1:


I think the problem is that your loop is

while(false) {

This loop never executes, because false != true. Consequently, the Java compiler is telling you that nothing in the body of the loop will ever execute, and hence it's unreachable.

Try changing your loop to

while (true) {

(the idiomatic "loop forever") and see if that fixes things.

Hope this helps!




回答2:


while(false) is always false and the loop body is never executed: unreachable. Change to while (true).




回答3:


The statement while(false) will never execute anything within that loop, thus it's all un-reachable.




回答4:


Sorry, but that is some smelly code. I'm not sure what the braces/blocks are doing after declaring your Color local vars, and after declaring your Rectangle var. The main problem for unreachability is while(false), which means it will never execute the associated block.



来源:https://stackoverflow.com/questions/9302133/why-is-this-code-giving-an-unreachable-statement-error

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