【问题标题】:do while loop doesn't loop even if the condition is true即使条件为真,做while循环也不循环
【发布时间】:2021-02-28 23:13:38
【问题描述】:

这是不循环的部分

import javax.swing.JOptionPane; // needed to use dialog boxs

public class PalindromeDetector
{// begin class
         
       public static void main(String[] args)
       {//begin main
          String userWord, continueLoop = " ";
          int numString;
           
          JOptionPane.showMessageDialog(null, "This program will ask the user for a string and check to see if it is a palindrome.");
                   
          do
          {//begin do while loop
             userWord = JOptionPane.showInputDialog("enter a string.");
             
             if(PalORNot(userWord))
                JOptionPane.showMessageDialog(null, userWord + " is a palinedrome.");
             else
                JOptionPane.showMessageDialog(null, userWord + " is not a palinedrome.");
                
             continueLoop = JOptionPane.showInputDialog("Would like to try again? enter yes to try again or quit to exit.");
                                                     
          } while (continueLoop == "Yes" || coninueLoop == "yes"); //end do while loop

【问题讨论】:

  • 第二次检查中的coninueLoop 是不是拼写错误?顺便说一句,最好将用户答案转换为大写,然后仅检查 YES。
  • 哦,我复制错了。即使 continueLoop 变量拼写相同,它仍然不会循环。
  • 检查这个。你应该使用continueLoop.equals("Yes") 而不是continueLoop == "Yes"stackoverflow.com/q/513832/260313
  • 是的,我写了,现在可以了,谢谢。

标签: loops while-loop


【解决方案1】:

在Java中continueLoop == "Yes"不起作用,你必须使用continueLoop.equals("yes")来比较字符串。

顺便说一句,你忘记了第二个continueLoop 中的t

==比较是否是同一个对象

.equals()比较是否是同一个值

【讨论】:

  • 我没有那样想。非常感谢它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 2014-03-30
  • 2016-03-14
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 2013-04-08
相关资源
最近更新 更多