使用 String.equals() 检查字符串是否为回文

发布于 2024-08-09 04:54:19 字数 496 浏览 2 评论 0原文

我有一个关于我正在编写的基本程序的问题,该程序询问诸如赛车之类的单词是否是回文。

我所有反转字符串、删除标点符号的方法都有效,但确定它是否为回文的方法却无效。

/**
* Determines if a series of letters makes a palinedrome
* 
* @param  str   All punctuation and spaces have been removed 
*               before this method is called.  
* @return true  if phrase is a palindrome,
*         false otherwise.
*/
public boolean isPalindrome(String str)
{
   String d = reverseString (str); 
   return( str.equals (reverseString (str) ) ); 

}

I had a question regarding a basic program I was writing said whether a word such as racecar is a palindrome or not.

All my methods which reverse the string, strip the punctuation work but the one that determines if it is a palindrome does not.

/**
* Determines if a series of letters makes a palinedrome
* 
* @param  str   All punctuation and spaces have been removed 
*               before this method is called.  
* @return true  if phrase is a palindrome,
*         false otherwise.
*/
public boolean isPalindrome(String str)
{
   String d = reverseString (str); 
   return( str.equals (reverseString (str) ) ); 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吝吻 2024-08-16 04:54:19

好吧,我不确定 d 在你的函数中的用途是什么,因为它从未被使用过,但是,如果你想知道为什么你的函数不起作用,只需添加调试代码:

public boolean isPalindrome (String str) {
    System.out.println ("DEBUG: original string = '" + str + "'");
    System.out.println ("DEBUG: reverse string = '" + reverseString (str) + "'");
    if (str.equals (reverseString (str)))
        System.out.println ("DEBUG: returning true");
    else
        System.out.println ("DEBUG: returning false");
    return str.equals (reverseString (str));
}

我敢打赌钱是因为您的 reverseString 函数出现问题(但不是太多钱)。这些调试语句应该足以让您找出问题所在。

Okay, I'm not sure what purpose d is meant to serve in your function since it's never used but, if you want to see why your function's not working, just add debug code:

public boolean isPalindrome (String str) {
    System.out.println ("DEBUG: original string = '" + str + "'");
    System.out.println ("DEBUG: reverse string = '" + reverseString (str) + "'");
    if (str.equals (reverseString (str)))
        System.out.println ("DEBUG: returning true");
    else
        System.out.println ("DEBUG: returning false");
    return str.equals (reverseString (str));
}

I'd bet money on there being something wrong with your reverseString function (but not much money). These debug statements should give you enough to figure out where the problem lies.

谁许谁一生繁华 2024-08-16 04:54:19

如果字符串reverseString(String string),并且所有空格都被删除,那么检查是否是回文应该被

public boolean isPalindrome(String string)
{
    return string.equals(reverseString(string));
}

授予这是区分大小写的,所以如果你的回文定义不关心大小写,那么使用 equalsIgnoreCase 代替。

如果这不起作用,那么您可能需要再次检查您的 stripping 和verseString 方法。

If string reverseString(String string), and all whitespace was removed then checking if something is a palindrome should be

public boolean isPalindrome(String string)
{
    return string.equals(reverseString(string));
}

Granted this is case sensitive so if your palindrome definition does not care about casing, then use equalsIgnoreCase instead.

If this does not work, then you may want to check your stripping and reverseString methods again.

淡墨 2024-08-16 04:54:19

您的问题是您没有显示的反向字符串方法。如果该方法工作正常,那么您的 isPalindrome 方法应该可以工作。您需要做的就是修复您的反向字符串方法。

Java 没有原生的反向字符串方法,我强烈建议您编写自己的方法。

然而,Java 有一个相反的方法 StringBufferStringBuilder。 StringBuilder 优于 StringBuffer。

使用 equals 方法将反转的字符串与原始字符串进行比较

Your problem is the reverse string method you have not shown. If that method is working properly then your isPalindrome method should work. All you need to do is fix your reverse string method.

Java does not have a native reverse string method, and I highly recommend you write your own.

Java does, however, have a reverse method for StringBuffer and StringBuilder. StringBuilder is preferred over StringBuffer.

Use the equals method to compare your reversed string to the original string

浮生面具三千个 2024-08-16 04:54:19

代码应该是这样的:

String d = reverseString (str); 
return( str.equals (d) ); 

你不必调用两次reverseString()

PS:StringBuffer 有一个反转字符串的方法。

The code should be like this:

String d = reverseString (str); 
return( str.equals (d) ); 

You don't have to call twice reverseString()

P.S.: StringBuffer has a method that reverses a String.

一刻暧昧 2024-08-16 04:54:19

我确信你现在已经提交了作业,但我正在学习 java 并且需要练习,所以这是我给你的代码。它使用 char 数组并将其反转。我认为最好的方法是使用 StringBuilder,但你的家庭作业的目的可能是学习自己做:

public class reverseString {
    public static void main(String[] args) {
        System.out.println("racecar is a palindrome: "+ isPalindrome("racecar"));
    }

    public static boolean isPalindrome(String str)
    {
       String d = reverseString (str); 
       return( str.equals (reverseString (str) ) ); 
    }

    private static char[] reverse(char[] input) {
        int length = input.length;
        char[] reversed = new char[length];
        for (int i=0;i<length;i++) {
            reversed[length-i-1]=input[i];
        }
        return reversed;
    }

    private static String reverseString(String input){
        String reversed = new String(reverse(input.toCharArray())); 
        return reversed;
    }   
}

输出:

racecar is a palindrome: true

如果有人对我的代码为何糟糕有任何评论,请走开。我将不胜感激任何建设性的批评。

I'm sure you've already submitted your homework by now, but I'm learning java and needed the practice, so here's my code for you. It uses a char array and reverses that. I'd assume the best way would be to use a StringBuilder, but the intention of your homework is probably to learn to do it yourself:

public class reverseString {
    public static void main(String[] args) {
        System.out.println("racecar is a palindrome: "+ isPalindrome("racecar"));
    }

    public static boolean isPalindrome(String str)
    {
       String d = reverseString (str); 
       return( str.equals (reverseString (str) ) ); 
    }

    private static char[] reverse(char[] input) {
        int length = input.length;
        char[] reversed = new char[length];
        for (int i=0;i<length;i++) {
            reversed[length-i-1]=input[i];
        }
        return reversed;
    }

    private static String reverseString(String input){
        String reversed = new String(reverse(input.toCharArray())); 
        return reversed;
    }   
}

Output:

racecar is a palindrome: true

If anyone has any comments on why my code sucks, fire away. I'd appreciate any constructive criticism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文