gpt4 book ai didi

java - 程序立即终止而不是停留在不断的循环中

转载 作者:行者123 更新时间:2023-12-01 09:14:39 26 4
gpt4 key购买 nike

我正在尝试制作一个程序,当按下按钮时,它将开始创建许多不可见的框架,以至于导调用脑崩溃。但是,当我尝试运行它时,控制台立即终止

这是该程序的代码:

 public class JavaTester extends JFrame {
static JFrame frame;
static ImageIcon img;
private static boolean a = false;

public JavaTester() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(670, 700);
frame.setLocationRelativeTo(null);
frame.getContentPane().setBackground(new Color(0,255,0,0));
frame.getContentPane().setLayout(null);
frame.setUndecorated(true);

frame.setResizable(false);
frame.setVisible(true);

addKeyListener(new KeyListener() {

public void keyPressed(KeyEvent e) {
a = true;

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}

});
}

public static void main(String[] args) {
do {

if (a) {
while (true)
new JavaTester();

}
} while (a = false);
}
}

这是我的循环排序方式的问题还是有其他原因导致它立即停止运行?

最佳答案

问题出在这里: while (a = false);

通过这样做,a 被设置为 false

相反,您需要使用 conditional operator 等于 (a == false)

关于java - 程序立即终止而不是停留在不断的循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40654337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com