2012-10-11 51 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

它很高興知道,你的問題得到解決。我只是試圖通過不改變任何現有條件並且不引入新變量來簡化解決方案。這兩個答案在概念上都是相似的:) –