为什么此代码会出现“无法访问的语句”错误?

这是我的代码,我得到一个无法访问的语句错误,但我不知道为什么。

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

确切的错误是:

 java:68: unreachable statement { ^ 

帮助会很好,这段代码应该循环,直到找到像素。

我认为问题在于你的循环是

 while(false) { 

这个循环永远不会执行,因为false != true 。 因此,Java编译器告诉您循环体中的任何内容都不会执行,因此它无法访问。

尝试将循环更改为

 while (true) { 

(惯用的“永远循环”),看看是否能解决问题。

希望这可以帮助!

while(false)始终为false且循环体永远不会执行: 无法访问 。 改为while (true)

while(false)语句永远不会在该循环中执行任何内容,因此它都是不可访问的。

对不起,但这是一些臭臭的代码。 在声明Color局部变量之后,以及在声明Rectangle var之后,我不确定大括号/块正在做什么。 不可达性的主要问题是while(false) ,这意味着它永远不会执行相关的块。