【发布时间】: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