使用 String.equals() 检查字符串是否为回文
我有一个关于我正在编写的基本程序的问题,该程序询问诸如赛车之类的单词是否是回文。
我所有反转字符串、删除标点符号的方法都有效,但确定它是否为回文的方法却无效。
/**
* 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我不确定 d 在你的函数中的用途是什么,因为它从未被使用过,但是,如果你想知道为什么你的函数不起作用,只需添加调试代码:
我敢打赌钱是因为您的
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: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.如果字符串reverseString(String string),并且所有空格都被删除,那么检查是否是回文应该被
授予这是区分大小写的,所以如果你的回文定义不关心大小写,那么使用 equalsIgnoreCase 代替。
如果这不起作用,那么您可能需要再次检查您的 stripping 和verseString 方法。
If
string reverseString(String string)
, and all whitespace was removed then checking if something is a palindrome should beGranted 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.
您的问题是您没有显示的反向字符串方法。如果该方法工作正常,那么您的 isPalindrome 方法应该可以工作。您需要做的就是修复您的反向字符串方法。
Java 没有原生的反向字符串方法,我强烈建议您编写自己的方法。
然而,Java 有一个相反的方法 StringBuffer 和 StringBuilder。 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
代码应该是这样的:
你不必调用两次reverseString()
PS:StringBuffer 有一个反转字符串的方法。
The code should be like this:
You don't have to call twice reverseString()
P.S.: StringBuffer has a method that reverses a String.
我确信你现在已经提交了作业,但我正在学习 java 并且需要练习,所以这是我给你的代码。它使用 char 数组并将其反转。我认为最好的方法是使用 StringBuilder,但你的家庭作业的目的可能是学习自己做:
输出:
如果有人对我的代码为何糟糕有任何评论,请走开。我将不胜感激任何建设性的批评。
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:
Output:
If anyone has any comments on why my code sucks, fire away. I'd appreciate any constructive criticism.