我有一个代码,它从 a 中获取一个值button
,然后输出一些东西。如果我不使用break
; 我按下left button
,它会输出数千个。enter
和相同right
。
我不是 Java 专家,几周前我才开始使用 Java 编程。
我希望我的代码永远不会到stop reading
. button value
,但我不希望我的代码在button is pushed
. 我怎样才能做到这一点?此代码有效,但在 I 之后停止push one button
。如果我向左按下按钮,它将向左输出一次,然后停止运行。没有休息;它将输出数千个左侧。
for (int i = 0; ; i++) {
// Get the data from analog input 5
int sensorValue1 = phidget.getSensorValue(1);
int sensorValue2 = phidget.getSensorValue(2);
int sensorValue3 = phidget.getSensorValue(3);
if (sensorValue1 > 100 || sensorValue2 > 100 || sensorValue3 > 100){
// printing value
//System.out.println("sensorValue1 = " + sensorValue1 + ", sensorValue2 = " + sensorValue2 + ", sensorValue3 = " + sensorValue3 + ", Count = " + i);
if (sensorValue1 > 100){
System.out.println("RIGHT");
// simulates RIGHT key
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_RIGHT);
} catch (AWTException e) {
e.printStackTrace();
}
break;
} else if (sensorValue2 > 100)
{
System.out.println("LEFT");
// simulates LEFT key
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_LEFT);
} catch (AWTException e) {
e.printStackTrace();
}
break;
} else if (sensorValue3 > 100)
{
System.out.println("ENTER");
// simulates ENTER key
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
break;
}
} else {}
}