Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 跳出for循环_Java - Fatal编程技术网

Java 跳出for循环

Java 跳出for循环,java,Java,我试着运行下面的代码,但是当选择单选按钮trueButton或falseButton时,它不会中断for循环 for(;;){ if (trueButton.isSelected() || falseButton.isSelected()){ System.out.print("Selected"); break; } } for(;;){ System.out.println(""); if (trueB

我试着运行下面的代码,但是当选择单选按钮trueButton或falseButton时,它不会中断for循环

for(;;){            
    if (trueButton.isSelected() || falseButton.isSelected()){
        System.out.print("Selected");
        break;
    }
}
for(;;){
    System.out.println("");
    if (trueButton.isSelected() || falseButton.isSelected()){
        System.out.print("Selected");
        break;
    }
}
但是,如果我添加System.out.println(“”);在if语句之前,当选择trueButton或false按钮时,我能够中断for循环

for(;;){            
    if (trueButton.isSelected() || falseButton.isSelected()){
        System.out.print("Selected");
        break;
    }
}
for(;;){
    System.out.println("");
    if (trueButton.isSelected() || falseButton.isSelected()){
        System.out.print("Selected");
        break;
    }
}
我是否可以在不在if语句之前添加System.out.println(“”)的情况下跳出循环

如果有人能解释为什么第一个代码不能工作,那就太好了

由于我是编程新手,请原谅我对任何事情一无所知

编辑:我在下面创建了一个类似的场景供大家测试

public static void main(String[] args){
    JFrame window = new JFrame("Hello");
    JRadioButton trueButton = new JRadioButton("True ");
    JRadioButton falseButton = new JRadioButton("False ");      

    window.setSize(400, 325);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    window.setLayout(new FlowLayout());
    window.add(trueButton);
    window.add(falseButton);

    trueButton.setVisible(true);
    falseButton.setVisible(true);
    window.setVisible(true);

    for(;;){
        if (trueButton.isSelected() || falseButton.isSelected()){
            System.out.println("Selected");
            break;
        }
    }
    System.out.println("Done");
}
如果我们设法打破循环,则应打印“完成”。

尝试以下操作:

  for(;;){            
            if (trueButton.isSelected() || falseButton.isSelected()){
            System.out.print("Selected");
            return;
        }
    }
或者这个:

outerloop:
  for(;;){            
            if (trueButton.isSelected() || falseButton.isSelected()){
            System.out.print("Selected");
            break outerloop;
        }
    }
否则看起来一切都设置正确。。。
希望这有助于

在编写GUI时,您应该避免不惜一切代价阻止循环,因为它会导致应用程序无响应

您应该添加一个ActionListener,以便在单击按钮时“作出反应”。否则,您只需使用
isSelected
方法检查单击哪个按钮,而不使用循环;例如,在提交表单或执行某些计算时


如果您确实需要不断读取按钮的状态,请使用

你为什么不使用while循环呢<代码>while(!trueButton.isSelected()&&&!false button.isSelected()){}System.out.print(“Selected”)