繁体   English   中英

Java回文程序(1个错误)

[英]Java Palindrome program (1 error)

我昨天问了一个关于回文和 Java 的问题:

Java 回文程序(我是否走上正轨)?

到目前为止,在您的帮助下,我已经取得了一些进展(再次非常感谢您)。 在我可以测试代码之前,我只需要另外一件事的帮助。 我正在使用 Eclipse,但在一行中出现错误(我还将将该错误作为注释包含在下面的代码中)。 .我不断收到

有谁知道这里发生了什么? 我已经有一段时间没有使用 Java 了。 大约 12 个月前在 CS One 中使用它,然后我在数据结构中转到 C++,然后在下一门课程中转到机器代码和汇编语言。 这是代码(我还在代码的注释中包含了错误)。 非常感谢:

public class Palindrome 
{

public boolean isPalindrome( String theWord )  
{    
    for ( int i = 0; i < theWord.length( ); i++ ) {
        if ( theWord.charAt(i) != theWord.charAt (theWord.length() - i - 1) ) {
            return false;
        }
    }
    return true;
}


public static void main( String [] theWord ) 
{
        int leftPointer = 0;
        int rightPointer = theWord.length - 1;

        for ( int i = 0; i < theWord.length / 2; i++ ) {
            while (leftPointer >= rightPointer) {
                if ( theWord.charAt(i) == theWord.charAt (theWord.length - i - 1) ) { // Error: Cannot invoke charAt(int) on the array type String[]
                    leftPointer++;
                    rightPointer--;
                }
                System.out.println(theWord);
            }
        }
}

}

您正在尝试访问 String[](传递给程序的参数的 String 数组)上的 charAt(),但您需要在 String 上访问它。 我的世界建议如下:

if ( theWord[i].charAt(0) == theWord[theWord.length - i - 1].charAt (0) ) {

那可能对你有帮助。

charAt(int index) 应用于字符串,而不是字符串数组。 你的程序想要判断一个字符串是否是回文,比如“abcba”。 而不是检查字符串数组是否都是回文,对吗? 例如 {"abcba", "bbc", "aba"}。

您在调用方法 .length() 后忘记了 ()

在 Java 中(如在 C++ 中)程序接收参数列表,它是字符串数组。 因此,您的课程应如下所示:

public class Palindrome 
{
  public static boolean isPalindrome( String theWord )  
  {    
    for ( int i = 0; i < theWord.length( ); i++ ) {
      if ( theWord.charAt(i) != theWord.charAt (theWord.length() - i - 1) ) {
        return false;
      }
    }
    return true;
  }


  public static void main( String [] args ) 
  {
    String theWord = args[0];  // first word passed to the program
    boolean isPalindrom = Palindrome.isPalindrome(theWord);
    System.out.println(theWord + " is" + ( isPalindrom ? "" : " NOT " )   + " a Palindrome." ); 
  }

}
public static boolean isPalindrom(String value) {
    if (value == null || value.length()==0 || value.length()==1) {
        return true;
    }
    if(value.charAt(0)!=value.charAt(value.length()-1)) {
        return false;
    }
    StringBuilder newValue =new StringBuilder(value);
    newValue = new StringBuilder(newValue.substring(1, newValue.length()));
    newValue = new StringBuilder(newValue.substring(0, newValue.length()-1));
    return isPalindrom(newValue.toString());
}

试试这个简单的递归方法。

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
      
      String str = "pappap";
      String sp = str.toLowerCase();
      char[] ch = sp.toCharArray();
      int len = ch.length;
      int lastIndex = ch.length-1;
      int count = 1;
      int first = 0;
      int last = sp.length()-1;
     for(; first < last  ;){  
              if(ch[first] == ch[last] ){
                  first= first+1;
                  last= last-1; 
               }
             else{
              count = 0;
              break;
              }
      }
      
    String result = (count == 1) ?  "Palindrome" : "Not a palindrome " ;
    System.out.println(result);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM