Java 为什么这段代码会出现“不可访问语句”错误?
这是我的代码,我得到一个无法访问的语句错误,但我不知道为什么Java 为什么这段代码会出现“不可访问语句”错误?,java,loops,while-loop,unreachable-code,Java,Loops,While Loop,Unreachable Code,这是我的代码,我得到一个无法访问的语句错误,但我不知道为什么 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 Rect
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;
}
}
帮助会很好,这段代码应该循环直到找到像素。我认为问题在于你的循环是错误的
while(false) {
此循环从不执行,因为false!=符合事实的因此,Java编译器告诉您,循环体中的任何内容都不会执行,因此它是不可访问的
尝试将循环更改为
while (true) {
这个惯用的循环会一直持续下去,看看是否能解决问题
希望这有帮助 whilefalse始终为false,并且循环体从未执行过:不可访问。更改为while true。whilefalse语句将永远不会在该循环中执行任何内容,因此它是不可访问的。抱歉,这是一些难闻的代码。在声明颜色局部变量和矩形变量后,我不确定大括号/块在做什么。不可访问性的主要问题是whilefalse,这意味着它永远不会执行关联的块。代码肯定不会进入循环whilefalse,因为它永远不会计算为true,所以不会进入它。这里面的返回语句有问题吗?你知道,false什么都不对吗?哦,我不觉得自己很愚蠢吗?false我在想什么?我的问题只是一个旁注
while (true) {