Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 而循环只运行一次_Java_Loops_While Loop - Fatal编程技术网

Java 而循环只运行一次

Java 而循环只运行一次,java,loops,while-loop,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 fr

我的问题是,当我运行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){
   break;
 }
exit=true将true指定给exit,因此如果条件为true,则它将中断循环

正如@jlordo所指出的,即使修复了错误,它也是一个无限循环。

您正在使用

if(exit = true) {
而不是

if(exit == true) {
甚至更好

if (exit) {
但是当您修复它时,您将有一个无限循环,因为所有的long值都小于9223372036854775807,这是long可以拥有的最高值

if(exit = true) 
这是第一个问题。上述if条件将始终为真。你需要做比较而不是赋值。事实上,您不应该对布尔变量进行比较。只需使用:-

if (exit)
其次,您不会在while循环的任何地方更改退出,因此,将其用作退出条件是没有意义的

您不需要硬编码Long.MAX\u value的值。您已经定义了该常量。现在,即使你解决了if的问题,你也会面临无限循环的问题。见下文:-

// 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;                        
      }                    
}

因为这一行:

if(exit = true)
您正在将true指定为退出此处。使用==比较值:

if(exit == true)
对于布尔值,根本不需要将其与true进行比较。这样写:

if(exit)

如果exit=true,则将值exit设置为true。你需要使用==

进行比较,为什么你要硬编码这么长的文字?在修复了明显的错误后,它将是一个无限循环。如果你使用long.MAX_值,你的代码会更清晰。没有其他人知道这个无限循环吗?@jlordo我刚刚意识到,是的,它是一个无限循环loop@jlordo不愿意;;{}还是说{}真的有更具可读性的替代品?@WilliamShatner。。我想,OP可能需要退出,并在循环中的某个地方更改退出。@RohitJain在这种情况下,他甚至不需要休息..:我认为他的意图是让他的停止按钮退出无限循环。@GangnamStyleOverflowerr.:从语义上讲,这不应该是一段时间吗!退出{…条件被翻转?或者实际上,可能是在运行{…?时,如果他用volatile标记布尔值,它会不会按照他想要的方式工作?@Clockwork Muse..关于语义,是的,应该是这样。但我刚刚映射了我的操作代码中使用的含义。
if(exit == true)
if(exit)