我遇到了我的代码问题。我知道答案是9,但我的代码输出0,我不知道为什么。我有两个类让它运行。一个方法类和一个Tester类来查看它是否工作。任何人都可以发现我的错误?为什么此代码打印出“0”
public class Robot
{
private int[] hall;
private int pos;
private boolean facingRight;
private boolean forwardMoveBlocked()
{
if (facingRight)
{
return pos == hall.length - 1;
}
else
{
return pos == 0;
}
}
private void move()
{
if (hall[pos] > 0)
{
hall[pos]--;
}
if (hall[pos] == 0
{
if (forwardMoveBlocked())
{
facingRight = !facingRight;
}
else
{
if (facingRight)
{
pos++;
}
else
{
pos--;
}
}
}
}
public int clearHall()
{
int count = 0;
while (!hallIsClear())
{
move();
count++;
}
return count;
}
public boolen hallIsClear()
{
return true;
}
}
这里是我的测试类代码
public class Tester
{
public static void main(String[] args)
{
Robot RobotTest = new Robot();
System.out.println(RobotTest.clearHall());
}
}
这里你缺少一个括号:'if(hall [pos] == 0' – Fenton
你是用调试器完成的吗? –
你在调试器中试过吗?它会马上向你显示这个问题。 –