2012-10-11 53 views
1

我有一个代码,从一个button值,然后输出的东西。 如果我不使用break;我按了left button,它会输出数千个左边。对于enterright也是如此。休息;然后继续;在java

我不是Java大师,几个星期前我开始使用Java编程。

我想我的代码从来没有stop readingbutton 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 {} 

    } 
+0

好吧,我不看你的背后最外层if结构.. –

+0

我真的不明白任何原因是什么你想.. –

+0

你不能在你的代码中添加eventHandling吗?什么时候按下哪个按钮? – Kent

回答

0

您可以跟踪sensorValue的最后处理值和点击被认为已经发生时sensorValue1的新值超过100,而旧的价值是在100

int oldSensorValue1 = 0; 
int oldSensorValue2 = 0; 
int oldSensorValue3 = 0; 
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 && oldSensorValue1 < 100){ 

      System.out.println("RIGHT"); 

      // simulates RIGHT key 
      try { 
      Robot robot = new Robot(); 
      robot.keyPress(KeyEvent.VK_RIGHT); 
      } catch (AWTException e) { 
      e.printStackTrace(); 
      } 

     } else if (sensorValue2 > 100 && oldSensorValue2 < 100) 
     { 
      System.out.println("LEFT"); 

      // simulates LEFT key 
      try { 
      Robot robot = new Robot(); 
      robot.keyPress(KeyEvent.VK_LEFT); 
      } catch (AWTException e) { 
      e.printStackTrace(); 
      } 
     } else if (sensorValue3 > 100 && oldSensorValue3 < 100) 
     { 
      System.out.println("ENTER"); 

      // simulates RIGHT key 
      try { 
      Robot robot = new Robot(); 
      robot.keyPress(KeyEvent.VK_RIGHT); 
      } catch (AWTException e) { 
      e.printStackTrace(); 
      } 
     } 
     oldSensorValue1 = sensorValue1; 
     oldSensorValue2 = sensorValue2; 
     oldSensorValue3 = sensorValue3; 
} 
+0

真棒这个工作很棒!它正在做我想要的!非常感谢你 – MOTIVECODEX

1

从所有if条件中删除break声明。它将从for循环中删除执行。

我只是说以重新初始化IF条件内的sensorvalues

如果(sensorValue1> 100){ ..... sensorValue1 = 0; }

对于sensorValue2

否则如果(sensorValue2> 100){ ..... sensorValue2 = 0; }

对于sensorValue3

如果(sensorValue3> 100){ ..... sensorValue3 = 0; }

+0

这没有奏效,但它现在已经修复了,谢谢你+1 – MOTIVECODEX

3

设置一个变量来指示最后一次输出是什么(“左”,“右”等)。然后,再次输出前,检查变量是否设置为您要输出的值。如果是,则跳过输出;如果不是,请执行输出并重置变量。

private static final String LEFT = "LEFT"; 
private static final String RIGHT = "RIGHT"; 
private static final String ENTER = "ENTER"; 

String lastOutput = null; 
for (i = 0; ; i++) { 
    . . . 
    if (sensorValue1 > 100){ 
     if (lastOutput != RIGHT) { 
      System.out.println(RIGHT); 
      lastOutput = RIGHT; 
     } 
    . . . 
} 
+0

+1这里最好的解决方案 – Baz

+0

这可能是有效的,但在看到这个之前我使用了Tom Gerken提供的代码。无论如何谢谢你和+1 – MOTIVECODEX

1

你什么需要做的是跟踪按钮值,并且只在按钮值低于100时打印右或左,然后高于100.当按钮值高于100时,需要等到它下降到100以下直到你再次检查它是否回到上面。

你需要保留一些状态变量每个按钮,很可能就像你当按钮变为高于100设置为truedidPrintMessage一个布尔值,并重置为false,当它低于100。然后,该按钮时的值超过100,只打印左或右,如果didPrintMessagefalse。在将didPrintMessage设置为true之前执行此操作。

boolean didPrintMessage1 = false; 
boolean didPrintMessage2 = false; 
boolean didPrintMessage3 = false; 
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 && !didPrintMessage1) { 
     System.out.println("RIGHT"); 
     /* Robot stuff */ 
    } else if (sensorValue2 > 100 !didPrintMessage2) { 
     System.out.println("LEFT"); 
     /* Robot stuff */ 
    } else if (sensorValue3 > 100 !didPrintMessage3) { 
     System.out.println("ENTER"); 
     /* Robot stuff */ 
    } 

    didPrintMessage1 = sensorValue1 > 100; 
    didPrintMessage1 = sensorValue2 > 100; 
    didPrintMessage1 = sensorValue3 > 100; 
} 

看起来像Java是在嵌入式系统上运行,就像微控制器一样。将来,这将是有用的信息。

+0

这可能有效,但在看到这个之前,我使用了Tom Gerken提供的代码。不管怎么说,谢谢你和+1 – MOTIVECODEX

1

我只是简单地将所有传感器值分配到0最后,并在开始时添加一个if条件。如果用户输入了两次相同的密钥而没有输入任何密码,这也将帮助您区分。我没有看到使用旧值变量的任何用法。

// Get the data from analog input 5 
int sensorValue1 = phidget.getSensorValue(1); 
int sensorValue2 = phidget.getSensorValue(2); 
int sensorValue3 = phidget.getSensorValue(3); 

if (sensorValue1 == 0 && sensorValue2 == 0 && sensorValue3 ==0){ 
    /don't do anything 
else if (sensorValue1 > 100 && oldSensorValue1 < 100){ 
...... 
...... 

在底部(出的if-else),加

sensorValue1 = 0; 
sensorValue2 = 0; 
sensorValue3 = 0; 
+0

这可能是有效的,但在看到这个之前,我使用了汤姆葛根提供的代码。无论如何谢谢你和+1 – MOTIVECODEX

+0

它很高兴知道,你的问题得到解决。我只是试图通过不改变任何现有条件并且不引入新变量来简化解决方案。这两个答案在概念上都是相似的:) –