我需要確保在每次分配時都必須編寫自己的原始代碼,而不是複製其他人的代碼。這看起來比你期望的要難。我試圖寫一個迴文檢測器作爲任務的一部分。代碼很好,除了一個問題。輸出結果表明它是真實的,即使它不是一個迴文序列,它以相同的字符開始和結束。請你幫我。這裏是我的代碼:即使不是迴文,迴文檢測器也會返回true
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));
}
的目標是獲得了這個詞的測試,這是不是一個迴文,註冊爲假,ABBA,這是一個迴文,註冊爲真,應用程序,這是不是一個迴文,註冊爲false。
你應該使用StringBuilder過的StringBuffer沒有需要同步開銷 – 2014-08-28 02:30:18
@ Smith_61其實我只是想複製錯誤的單詞,謝謝。 – 2014-08-28 02:37:45