2015-01-04 75 views
-1

当我尝试运行这个程序时,发生无限循环。我无法确定问题出在主要方法还是递归方法中。回文递归 - 无限循环

这里是递归方法。

public class RecursivePalindrome 
{ 
public boolean isPalindrome(String s) 
{ 
    if(s.length() <= 1) 
    { 
     return true; 
    } 
    else if(s.charAt(0) == s.charAt(s.length() - 1)) 
    { 
     return isPalindrome(s.substring(1,s.length() - 1)); 
    } 
    else 
    { 
     return false; 
    } 
} 
} 

这里是主要的方法。

public class RecursivePalindromeTester 
{ 
public static void main(String[] args) 
{ 
    Scanner in = new Scanner(System.in); 
    System.out.print("Enter a word or phrase. Type Q to quit: "); 
    String word = in.next(); 
    RecursivePalindrome object = new RecursivePalindrome(); 
    while(!word.equalsIgnoreCase("Q")) 
    { 
     if(object.isPalindrome(word)) 
     { 
      System.out.println(word + " is a palindrome"); 
     } 
     else 
     { 
      System.out.println(word + " is not a palindrome"); 
     } 
    } 
    System.out.print("Enter another word or phrase. Type Q to quit: "); 
    word = in.next(); 
} 
} 
+0

@lurker'endIndex'是唯一的。 – user2336315

回答

3

看来,你永远不能摆脱你的主要方法while循环。只是你在错误的地方作出的(你:

您应该将这些两行进入while循环,使用户可以进入别的东西,或键入“q”退出:

System.out.print("Enter another word or phrase. Type Q to quit: "); 
word = in.next(); 
+0

这工作得很好。谢谢你的帮助! –

+0

@ TommyLam接受这个答案。 –

+0

@TommyLam很高兴我能帮忙! –

0

简单的解决方案忘记将单词变量的更新包含到while循环中)。所以只是一个错字:

public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.print("Enter a word or phrase. Type Q to quit: "); 
     String word = in.next(); 
     RecursivePalindrom object = new RecursivePalindrom(); 
     while(!word.equalsIgnoreCase("Q")) 
     { 
      if(object.isPalindrome(word)) 
      { 
       System.out.println(word + " is a palindrome"); 
      } 
      else 
      { 
       System.out.println(word + " is not a palindrome"); 
      } 
     // } old postion of the brace 
     System.out.print("Enter another word or phrase. Type Q to quit: "); 
     word = in.next(); 
     } // new position of the brace 
    } 
+0

这工作得很好。谢谢你的帮助! –

+0

如果有效,请接受它:D – ProgrammingIsAwsome

0

问题是,你从主输入后输入的下一个单词循环。所以这个词不会改变,并永远循环。

+0

这工作得很好。谢谢你的帮助! –

1

你问题就出在这条线的位置:

while(!word.equalsIgnoreCase("Q")) 
    { 
     if(object.isPalindrome(word)) 
     { 
      System.out.println(word + " is a palindrome"); 
     } 
     else 
     { 
      System.out.println(word + " is not a palindrome"); 
     } 
    } 
    System.out.print("Enter another word or phrase. Type Q to quit: "); //problem 
    word = in.next(); 

您runninging什么也没有while循环更新

更正:

while(!word.equalsIgnoreCase("Q")) 
    { 
     if(object.isPalindrome(word)) 
     { 
      System.out.println(word + " is a palindrome"); 
     } 
     else 
     { 
      System.out.println(word + " is not a palindrome"); 
     } 
     System.out.print("Enter another word or phrase. Type Q to quit: "); 
     word = in.next(); 
    } 

正如一个建议,查找Java编码约定,例如{通常留在行尾,而不是自己,将代码基于约定使其他人更容易阅读

1

这诡计循环是无限的

while(!word.equalsIgnoreCase("Q")) 
    { 
     if(object.isPalindrome(word)) 
     { 
      System.out.println(word + " is a palindrome"); 
     } 
     else 
     { 
      System.out.println(word + " is not a palindrome"); 
     } 
    } 

while循环不断循环,直到字设置为“Q”,这发生在循环外,将此代码:

System.out.print("Enter another word or phrase. Type Q to quit: "); 
    word = in.next(); 

在你的while循环结束

+0

这工作得很好。谢谢你的帮助! –