我应该让我的雀跟随光源,具有以下要求:
- 使雀向光源移动 - 检测到光源时将 LED 颜色设置为蓝色
- 如果没有光源,芬奇不会动。
- 将 LED 颜色设置为红色。
但我在最后一点遇到语法错误。
myf.quit(); (unreachable code is the error)
而且我的代码似乎也没有执行所需的任务。问题会出在哪里?
我的代码:
import edu.cmu.ri.createlab.terk.robot.finch.Finch;
public class Myfinchtask {
public static void main(String[] args) {
Finch myf = new Finch();
int[] lightsensors = myf.getLightSensors();
int meetsthreshold = 300;
int lightsensorsaverage = 0;
while(true){
lightsensors = myf.getLightSensors();
lightsensorsaverage = (lightsensors[0]+lightsensors[1])/2;
if (lightsensorsaverage < meetsthreshold)
{
myf.stopWheels();
myf.setLED(100, 0, 0);
while (lightsensorsaverage < meetsthreshold){
lightsensors = myf.getLightSensors();
lightsensorsaverage = (lightsensors[0]+lightsensors[1])/2;
if (lightsensorsaverage > meetsthreshold){
myf.setLED(0, 0, 100);
myf.setWheelVelocities(100, 100);
}
}
}
}
myf.quit();
System.exit(0);
}
}