0

我正在为一堂课做这个,我真的不是很厉害,因为我已经十多年没有练习了。我正在尝试编写一个显示菜单的程序,以便用户可以在确定它是否是回文的方法之间进行选择。

完成测试后,它需要重新显示菜单。我在 isPalindrome 方法中遇到堆栈溢出错误,因为我将 2 个类合并为一个类,我认为这可以解决我在输出中遇到的另一个问题!我可以采取任何想法或方向吗?

import java.util.Scanner;

public class PalHelper 
{
public String pal;
public void MenuList() 
{
System.out.println("How would you like to check your phrase?");
System.out.println("1. Check the first letter like it's the last letter - Leave no phrase unturned!");
System.out.println("2. I Prefer my Palindromes have the gentle treatment");
System.out.println("3. We're done here");
System.out.print("Selection: ");
}

public PalHelper() 
{
Scanner decision = new Scanner(System.in);
    MenuList();

switch (decision.nextInt()) 
{
    //to access the character by character method of determination
    case 1:
    System.out.print("Enter Phrase to Test, the Hard Way:");
    Scanner keyboard1 = new Scanner(System.in); //declares scanner variable for user entry
    String UserInput1 = keyboard1.next();//Phrase variable
    Boolean test1 = isPalindrome(UserInput1);
            if (test1 == true){
                System.out.println(UserInput1+" is a palindrome. That doesn't make you smart.");    
                        }
            else {
                System.out.println(UserInput1+" is a not palindrome. Why don't you think a little harder and try again.");
                        }
    System.out.println("..\n..\n..\n");
    keyboard1.close();
    new MenuList();
    break;

    //to access the string buffer method of determination
    case 2:
    System.out.print("Thank you for choosing the gentle way, please enter your phrase:");
    Scanner keyboard2 = new Scanner(System.in); //declares scanner variable for user entry
    String UserInput2 = keyboard2.next();
    Boolean test2 = isPalindrome2(UserInput2);
    if (test2 == true){
                System.out.println(UserInput2+" is a palindrome. Congratulations! You are so wonderful!");  
                    }
            else {
                System.out.println(UserInput2+" is a not palindrome. It's ok, I'm sure you'll get it next time.");
                    }
    System.out.println("..\n..\n..\n");
    keyboard2.close();
    new MenuList();
    break;

//exit menu
    case 3:
    System.out.println ( "Too bad – I hid a boot!" );    
    break;

//response to input other than 1,2,3
    default:
    System.out.println ( "No sir! Away! A papaya war is on." );
    System.out.println("..\n..\n..\n");
    new MenuList();
    break;

    }// close switch
}//close pal helper




public void Palindrome(String UserInput) {

}

public boolean isPalindrome(String UserInput) {
pal = UserInput.toUpperCase();   
if (pal.length() <= 1) {//one character, automatically a palindrome
   return true;     
}

char start = pal.charAt(0);
char end = pal.charAt(pal.length()-1);
        if (Character.isLetter(start) &&
            Character.isLetter(end)) {//check if first and last characters match

            if (start != end) {
                return false;     //if the beginning & ending characters are not the same it's not a palindrome      
            }
            else {

            Palindrome subpal = new Palindrome(pal.substring(1,pal.length()-1));
            return subpal.isPalindrome();  //check middle dropping start and end letters
            }
        }
        else if (!Character.isLetter(start)) {
            Palindrome subpal = new Palindrome(pal.substring(1));
            return subpal.isPalindrome(pal);      //check if first letter is a letter, drop if not
        }
        else {
            Palindrome subpal = new Palindrome(pal.substring(0,pal.length()-1));
            return subpal.isPalindrome(pal);      //check if first letter is a letter, drop if not
        }                       

}//close isPalindrome
public boolean isPalindrome2(String UserInput){

 pal = UserInput.toUpperCase();
 pal = pal.replaceAll("\\W", "");//gets rid of space and punctuation
 StringBuffer check = new StringBuffer(pal);//reverses pal string and creates new stringbuffer for check
 check.reverse();


        if (check.toString().equals(pal)){//checks for equality between pal and it's reverse
            return true;
        }
        else {
            return false;
        }

  }//close isPalindrome2

public static void main (String[]args) 
{
new PalHelper();

}//close main
}//close class
4

2 回答 2

0

除了我上面的评论之外,我注意到了一个问题:在某些情况下,您使用子字符串构造了一个新的回文实例,但在递归到isPalindrome()方法时传递了完整的字符串。这会导致递归永远不会终止(这也使您的代码难以遵循)。

于 2013-02-14T20:31:46.547 回答
0
  1. 您的示例缺少某些内容,以下行缺少右括号:

    回文 subpal = new Palindrome(pal.substring(1,pal.length()-1);

  2. 您的评论似乎与代码不符:

    返回 subpal.isPalindrome(pal); //检查第一个字母是否是字母,如果不是则丢弃

  3. 尝试将输出添加到方法isPalindrome()或调试它。您可能没有使用正确的字符串调用它,并最终一遍又一遍地循环相同的字符串。

    public boolean isPalindrome(String UserInput) { System.out.println(UserInput); ...

编辑:如果您的代码确实像您发布的那样精确,那么 vhallac 是正确的,您可以isPalindrome()使用完整的字符串调用。

于 2013-02-14T20:34:43.493 回答