我的問題是我的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);
}
}
爲什麼你難以編碼這個醜陋的長文字? –
修復了顯而易見的錯誤之後,它將是**無限循環**。 – jlordo
如果您改用Long.MAX_VALUE,您的代碼會更清晰。 – whiskeyspider