【问题标题】:my if statement in my while loop does not activate on its own in Java?我的while循环中的if语句在Java中不会自行激活?
【发布时间】:2021-03-11 01:25:35
【问题描述】:

好的,所以我在 Java 中遇到了这个奇怪的故障,我已经解决了这个问题,但我不明白为什么会出现这个问题。所以我有一个while循环,循环一直在等待我的字符串变量“input”发生变化,当按下回车键时输入会发生变化。当布尔值“等待”为真时,while 循环继续循环。所以 while 循环不断循环,其中有一个 if-else 语句,if 语句说如果“输入”仍然是“”,那么“等待”继续等于真。否则,等待 = 假。但是,当这是循环时,我可以在输入将读取的字段中输入文本并按 Enter,但是当我执行任何操作时,什么也没有发生。除非我的 while 循环中有其他代码。任何其他似乎以某种方式输出文本的代码都可以解决这个问题,我不明白为什么。我将展示非工作代码和工作代码示例。

不工作

boolean waiting = true;
while(waiting)
{
    if(input.equals(""))
        waiting = true;
    else
        waiting = false;
    //System.out.println("waiting");
}

工作

boolean waiting = true;
while(waiting)
{
    if(input.equals(""))
        waiting = true;
    else
        waiting = false;
    System.out.println("waiting");
}

我不明白为什么这解决了这个问题,它没有给程序增加任何功能,我只是用它来查看循环是否正在循环,突然整个问题消失了,为什么要解决这个问题?

【问题讨论】:

  • 尝试在你的 if else, if(condition){code}else{morecode} 上放置块
  • 没有足够的上下文来回答。什么是input,是怎么声明的?写什么?这可能来自线程饥饿;第一个不工作的繁忙循环无所事事,任何写到input 的东西都没有机会运行——但是第二个写输出,这可以使线程屈服,另一个线程有机会改变输入。尝试将您的 println 替换为 Thread.currentThread().yield();
  • 没有什么可修复的。假设input 变量为空(Null 字符串),代码可以正常工作。同样,这是假设,因为您没有提供关于 input 的用途或填充方式的任何线索。两个代码版本都做同样的事情。
  • @StephenP 你所说的有效,我发布了一个答案,它还为代码提供了更多上下文,非常感谢。

标签: java loops while-loop


【解决方案1】:

好的,所以评论的人能够给我一个可行的解决方案,他们也解释了这个故障。 Stephen P 认为这可能是线程饥饿,他建议将我的打印语句替换为

Thread.currentThread().yield();

这解决了问题,感谢 Stephen P 的帮助。

对于那些之前在程序中询问上下文的人,“输入”被设置为“”,所以它不为空,当按下回车键时,会有这样的代码:

public helloWorld(){
    jta.setEditable(false);
    stats.setEditable(false);
    add(jtf, BorderLayout.SOUTH);
    add(jta, BorderLayout.CENTER);
    add(stats, BorderLayout.EAST);
    stats.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(10,10,10,10)));
    jta.setBorder(BorderFactory.createCompoundBorder(border, BorderFactory.createEmptyBorder(10,10,10,10)));
    jtf.setFont(jtf.getFont().deriveFont(Font.ROMAN_BASELINE, 20f));
    jtf.setBorder(new LineBorder(Color.BLACK, 2));
    
    jta.setText("The game is now beginning\n"
            + "What name do you want to give your character?\n");
    stats.setText("");
    
    jtf.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            input = jtf.getText();
            //ystem.out.println("heard");
        }
    });
}

其中 jtf 是用户输入文本的 java 文本字段。这是在 main 方法中使用的。

public static void main(String[] args){
    
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            frame = new helloWorld();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            //System.out.println("listening");
        }
    });

    input = "";
    playGame();
}

最终确实是线程耗尽,只是循环运行得太快,因此输入永远没有机会更新。再次感谢斯蒂芬 P

构造函数也被称为 hello world 因为我只有一个 hello world 文件不断准备好作为测试文件,是的,我知道它看起来不合适我很抱歉。对不起,我应该提前提供更多背景信息。

【讨论】:

  • 很高兴你想通了——但现在你应该研究更多;有更好的方法来对 Swing JTextField 变化(*)做出反应,除此之外,忙循环通常是 A Bad Thing™——它们在午餐时吃 CPU 周期......(*)(我还没有做过 Swing年龄,或者我会提出一些建议。EventListener?wait and notify?)
  • @StephenP 这个程序最初看起来大不相同,它没有任何循环,但该系统杂乱无章,比目前的系统要糟糕得多。我会考虑其他一些选择,因为这仍然不是很好。
猜你喜欢
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多