【问题标题】:break; then continue; in java [closed]休息;然后继续;在java中[关闭]
【发布时间】:2012-10-11 14:57:31
【问题描述】:

我有一个代码,它从button 中获取一个值,然后输出一些东西。 如果我不使用break;我按下left button,它会输出数千个。 enterright 也是如此。

我不是 Java 专家,几周前我才开始使用 Java 编程。

我希望我的代码永远不会stop readingbutton value,但我不希望我的代码在button is pushed 时输出数千个左右或输入。我怎样才能做到这一点?此代码有效,但在我 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 {}

    }

【问题讨论】:

  • 好吧,我看不出你最外层的任何原因,如果构造..
  • 我真的不明白你想要什么..
  • 不能在代码中添加 eventHandling 吗?按下哪个按钮时做什么?

标签: java break continue


【解决方案1】:

设置一个变量来指示最后的输出是什么(“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;
        }
    . . .
}

【讨论】:

  • 这可能有效,但在看到这个之前,我使用了 Tom Gerken 提供的代码。无论如何谢谢你和+1
【解决方案2】:

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

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

如果(传感器值 1 > 100){ ...... 传感器值1 = 0; }

对于sensorValue2

else if (sensorValue2 > 100){ ...... 传感器值2 = 0; }

对于sensorValue3

如果 (sensorValue3 > 100){ ...... 传感器值3 = 0; }

【讨论】:

  • 这不起作用,但现在已修复,谢谢+1
【解决方案3】:

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

您需要为每个按钮保留一些状态变量,可能只是一个布尔值,例如 didPrintMessage,当按钮超过 100 时设置为 true,当按钮低于 100 时重置为 false。然后,当按钮的值超过 100 时,如果 didPrintMessagefalse,则仅打印 LEFT 或 RIGHT。在将 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。将来,这将是有用的信息。

【讨论】:

  • 这可能有效,但在看到这个之前,我使用了 Tom Gerken 提供的代码。无论如何谢谢你和+1
【解决方案4】:

最后我只需将所有传感器值分配给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;

【讨论】:

  • 这可能有效,但在看到这个之前,我使用了 Tom Gerken 提供的代码。无论如何谢谢你和+1
  • 很高兴知道,您的问题已解决。我只是试图通过不改变任何现有条件并且不引入新变量来简化解决方案。两个答案在概念上相似:)
【解决方案5】:

您可以跟踪 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;
}

【讨论】:

  • 太棒了,这个效果很好!它正在做我想要的!非常感谢
猜你喜欢
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多