即使条件为真,do while 循环也不会循环

时间:2021-02-28 23:13:38

标签: loops while-loop

这是不循环的部分

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

1 个答案:

答案 0 :(得分:2)

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

顺便说一下,您在第二个 t 中忘记了 continueLoop

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

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