1

我有一个代码,它从 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 {}

    }
4

5 回答 5

3

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

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;
        }
    . . .
}
于 2012-10-11T15:03:32.260 回答
1

从所有条件中删除该break语句。if它正在从 for 循环内部删除执行。

我只想说重新初始化 IF 条件内的传感器值

if (sensorValue1 > 100){ ..... sensorValue1 = 0; }

对于传感器值2

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

对于sensorValue3

if (sensorValue3 > 100){ ..... sensorValue3 = 0; }

于 2012-10-11T15:03:28.353 回答
1

您需要做的是跟踪按钮值,仅在按钮值低于 100 时打印 RIGHT 或 LEFT,然后高于 100。当按钮值高于 100 时,您需要等到它返回到下方100 直到你再次检查它是否回到上面。

您需要为每个按钮保留一些状态变量,可能只是一个布尔值didPrintMessagetrue当按钮高于 100 时设置为,并在低于 100 时重置为false。然后,当按钮的值高于 100 时,仅打印LEFT 或 RIGHT 如果didPrintMessagefalsedidPrintMessage在设置为之前执行此操作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,比如微控制器。将来,这将是有用的信息。

于 2012-10-11T15:06:34.063 回答
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;
于 2012-10-11T15:12:41.947 回答
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;
}
于 2012-10-11T15:04:49.883 回答