Len*_*och 0 java loops while-loop
我的问题是,当我运行它时,我的while循环只运行一次.这是我的代码.如果有人检查它并且可能寻找其他错误(我是编程java的新手!),我将非常感激.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Program {
boolean exit;
JFrame frame;
JPanel panel;
JTextField title;
JButton start;
JButton stop;
long x;
public Program() {
frame = new JFrame("Überlast your PC");
panel = new JPanel();
title = new JTextField("Überlast your PC v1.0");
start = new JButton("Start");
stop = new JButton("Stop");
x = 1;
exit = false;
stop.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
exit = true;
}});
start.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
panel.remove(start);
panel.add(stop);
frame.repaint();
frame.revalidate();
start.setForeground(Color.red);
while(x <= 9223372036854775807L) {
System.out.println(x * x);
x++;
if (exit = true) {
break;
}
}
}});
frame.add(panel);
panel.add(title);
panel.add(start);
frame.setVisible(true);
frame.setSize(150,100);
title.setEditable(false);
start.setForeground(Color.green);
stop.setForeground(Color.red);
}
}
Run Code Online (Sandbox Code Playgroud)
if(exit = true) {
break;
}
Run Code Online (Sandbox Code Playgroud)
应该
if(exit == true) {
break;
}
Run Code Online (Sandbox Code Playgroud)
甚至更简单,只需使用
if(exit){
break;
}
Run Code Online (Sandbox Code Playgroud)
exit=true为exit 指定true,因此if条件为true,因此它会突破循环.
正如@jlordo指出的那样,即使在修复错误之后它也是一个无限循环.
| 归档时间: |
|
| 查看次数: |
1319 次 |
| 最近记录: |