回文递归 - 无限循环

时间:2015-01-04 18:27:12

标签: java

当我尝试运行此程序时,会发生无限循环。我不知道问题是在主方法还是在递归方法中。

这是递归方法。

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();
}
}

5 个答案:

答案 0 :(得分:3)

看来你永远无法在主方法中突破你的while循环。

您应该将这两行移动到while循环中,以便用户可以输入其他内容或键入&#34; q&#34;退出:

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

答案 1 :(得分: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();

你正在运行一个没有任何更新的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编码约定,例如{通常留在行的末尾,而不是单独使用,基于约定使代码更容易让其他人阅读

答案 2 :(得分: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循环结束时

答案 3 :(得分: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
    }

答案 4 :(得分:0)

问题是你在主循环后从输入中得到下一个单词。所以这个词不会被改变并永远循环。