我的代码遇到了问题。我知道答案是 9,但我的代码打印出 0,我不知道为什么。我有 2 节课让它运行。一个方法类和一个Tester类,看看它是否有效。谁能发现我的错误?
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() );
}
}