我有一个代码,从一个button
值,然后输出的东西。 如果我不使用break
;我按了left button
,它会输出数千个左边。对于enter
和right
也是如此。休息;然后继续;在java
我不是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 {}
}
好吧,我不看你的背后最外层if结构.. –
我真的不明白任何原因是什么你想.. –
你不能在你的代码中添加eventHandling吗?什么时候按下哪个按钮? – Kent