replaceAll 和循环?

replaceAll and loops?

我的计算机科学 class 作业要求我编写一个程序来确定单词或短语是否为回文(向前和向后相同,即 "noon")。作为其中的一部分,我必须编写一个删除所有标点符号和 space 的方法,因此在确定它是否为回文时不计算它们。它还循环运行,允许用户输入他们想要的任意数量的短语,直到他们表示完成。我的问题是,当输入的 word/phrase 包含 space 时,它会以某种方式终止循环并且不允许更多输入。只要输入没有 spaces,该程序就可以正常工作。这是我的代码:

在class递归回文中:

public String removePunctuation(String s){
    s = s.replaceAll("\.","");
    s = s.replaceAll("!","");
    s = s.replaceAll(",","");
    s = s.replaceAll(" ","");
    s = s.replaceAll("'","");
    s = s.replaceAll("-","");
    s = s.replaceAll("\?","");
    return s;
}

public boolean isPalindrome(String s) {

    s = removePunctuation(s);

    String firstChar = s.substring(0,1);
    String lastChar = s.substring(s.length()-1);

    if (s.length() == 1){
        return true;
    }

    if (s.length() == 2 && firstChar.equalsIgnoreCase(lastChar)){
        return true;
    }

    if (!firstChar.equalsIgnoreCase(lastChar)){
        return false;
    }

    return isPalindrome(s.substring(1, s.length() - 1));
}

在class递归回文测试器中:

public static void main(String[]args){

    //Create objects
    Scanner in = new Scanner(System.in);
    RecursivePalindrome palindrome = new RecursivePalindrome();

    //Output
    for (String again = "Y"; again.equalsIgnoreCase("Y"); again = in.next())
    {
        //Prompt for input
        System.out.println();
        System.out.print("Enter a word or phrase: ");
        String phrase = in.next();

        //Output
        if (palindrome.isPalindrome(phrase)){
            System.out.println("This is a palindrome.");
        }
        else
            System.out.println("This is not a palindrome.");

        System.out.print("Another word or phrase? (Y/N): ");
    }
}

输出应该是:

"Enter word or phrase: <input>mom- mom!
This is a palindrome
Another word or phrase? (Y/N): <input>Y
Enter a word or phrase: <input>Dog?
This is not a palindrome
Another word or phrase? (Y/N): <input>N"
Terminate

但我得到的是:

"Enter word or phrase: <input>mom- mom!
This is a palindrome
Another word or phrase? (Y/N):"
Terminate

我真的不知道为什么 space 会导致循环终止,尤其是因为它不使用任何其他标点符号来执行此操作。

完全同意@Ilya Bursov 的评论,

你应该使用 in.nextLine() 而不是 in.next() ,这两种方法有很大的区别

next()只能读取到space的输入。它无法读取由 space 分隔的两个单词。此外,next() 在读取输入后将光标置于同一行。

nextLine() 读取包含单词之间 space 的输入(即,它读取到行尾 \n)。读取输入后,nextLine() 将光标定位在下一行

这样试试,

class RecursivePalindromeTester {

  public static void main(String[] args) {

    //Create objects
    Scanner in = new Scanner(System.in);
    RecursivePalindrome palindrome = new RecursivePalindrome();

    //Output
    for (String again = "Y"; again.equalsIgnoreCase("Y"); again = in.nextLine()) {
      //Prompt for input
      System.out.println();
      System.out.print("Enter a word or phrase: ");
      String phrase = in.nextLine();

      //Output
      if (palindrome.isPalindrome(phrase)) {
        System.out.println("This is a palindrome.");
      }
      else
        System.out.println("This is not a palindrome.");

      System.out.print("Another word or phrase? (Y/N): ");
    }
  }
}