【问题标题】:Palindrome detector returns true even when it's not a palindrome即使不是回文,回文检测器也会返回 true
【发布时间】:2014-08-28 02:10:38
【问题描述】:

我需要确保在我所做的每项作业中,我都必须编写自己的原始代码,而不是抄袭别人的。这似乎比你想象的要难。我正在尝试编写一个回文检测器作为作业的一部分。代码很好,除了一个问题。输出表明这是真的,即使它不是回文并且它以相同的字符开头和结尾。请你帮助我。这是我的代码:

public static boolean isPalindrome_nr(String word){
    int beginning = 0;
    int end = word.length() - 1;

    boolean pd = true;

    for (int i = end; i>0; i--){
        if(word.charAt(0) == word.charAt(word.length()-1)){
            pd = true;
        }
        else if (word.charAt(0) != word.charAt(word.length()-i)){
            pd = false;
        }
    }

    return pd;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner scan = new Scanner(System.in);
    System.out.println("Is the string a palindrome or not? ");
    String test = scan.nextLine();
    System.out.println("Answer: " + isPalindrome_nr(test));
}

目标是得到单词test,不是回文,注册为假,abba,是回文,注册为真,application,不是回文,注册为假。

【问题讨论】:

    标签: java database eclipse boolean palindrome


    【解决方案1】:

    您只是比较第一个和最后一个字符。这还不足以确定一个字符串是否是回文。

    你需要这样的东西:

    pd = true;
    for (int i = end; i>=0; i--){
        if(word.charAt(i) != word.charAt(end-i)){
            pd = false;
            break;
        }
    }
    

    这可以进一步改进,因为这个循环会测试所有对两次,所以 i 在 end/2 或 (end/2)+1 结束可能就足够了。

    【讨论】:

      【解决方案2】:

      您只检查第一个和最后一个字符。该方法应该如下所示,以便您的 for 循环实际上执行它应该做的事情:

      public static boolean isPalindrome_nr(String word) {
          int beginning = 0;
          int end = word.length() - 1;
      
          boolean pd = true;
      
          for (int i = end; i > 0; i--) {
              // notice the use of i in here so that it will check all opposite chars
              if(word.charAt(i) == word.charAt(word.length() - 1 - i)) {
                  pd = true;
              }
              else { // don't need the else-if
                  pd = false;
              }
          }
      
          return pd;
      }
      

      作为一个额外的说明,还有另一种方法来测试一个字符串是否是回文:反转它并测试反转的字符串是否等于原始字符串。像这样(单行):

      public static boolean isPalindrome(String s) {
          return new StringBuilder(s).reverse().toString().equals(s);
      }
      

      或者更长的方式(使用 for 循环反转字符串,而不是使用 StringBuilder#reverse() 方法

      public static boolean isPalindrome(String s) {
          StringBuilder reverseString = new StringBuilder();
      
          // reverse the string
          for (int i = s.length() - 1; i > -1; i--) {
              reverseString.append(s.charAt(i));
          }
      
          // return whether or not the reversed string is equal to the original string
          return reverseString.toString().equals(s);
      }
      

      【讨论】:

      • 你应该使用 StringBuilder 而不是 StringBuffer。无需同步开销。
      • @Smith_61 其实我是故意的。刚才抄错字了。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 2018-05-29
      • 2012-09-26
      • 1970-01-01
      相关资源
      最近更新 更多