簡體   English   中英

循環只運行一次

[英]While Loop just runs one time

我的問題是,我的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);

    }

}
 if(exit = true) {
      break;
 }

應該

 if(exit == true) {
      break;
  }

甚至更簡單,只需使用

 if(exit){
   break;
 }

exit=true分配真實退出,因此,如果條件為真因此它打破失控循環。

正如@jlordo指出的那樣,即使在修復錯誤之后,也仍然存在無限循環。

if(exit = true) 

這是第一個問題。 上面的if條件將始終為true。 您需要進行比較而不是分配。 實際上,您不應該對布爾變量進行比較。 只需使用:-

if (exit)

其次,您不會在while循環中的任何地方更改exit ,因此,將其用作退出條件沒有任何意義。

而且您不需要對Long.MAX_VALUE的值進行硬編碼。 您已經定義了該常數。 現在,即使您解決了if的問題,您也將面臨infinite loop的問題。 參見下面的方法:-

// Your below while loop is `infinite`
// every long value will satisfy this condition
while(x <= Long.MAX_VALUE) {  // Don't hard code max long value

      System.out.println(x * x);
      x++;

      // You are not changing `exit` anywhere, so this check is absurd.   
      if (exit) {  
          break;                        
      }                    
}

您可能想要什么:-

可能您想無限循環運行,直到exit值為false為止。 在這種情況下,只需使用while (true) 並在循環中的某處更改exit的值。

實際上,如果您使用exit作為退出條件,則可以將while循環更改為:-

while (exit) {
    System.out.println(x * x);

    if (someCondition) {
        exit = false;
    }

}

您正在使用

if(exit = true) {

代替

if(exit == true) {

甚至更好

if (exit) {

但是,在修復它時,您將有一個無限循環 ,因為所有long值都小於9223372036854775807 ,這是long可以擁有的最大值。

因為這一行:

if(exit = true)

分配 true exit此處。 使用==比較值:

if(exit == true)

對於布爾值,您根本不需要將其與true進行比較。 像這樣寫:

if(exit)

if (exit = true)會將值exit設置為true。 您需要使用==進行比較

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM