所以我決定打印一個字符串中的所有迴文詞。我沒有使用數組或只找到3-character-words.Here的迴文是我的代碼的方法,問題是,它打印出什麼如何在字符串中打印迴文單詞?
import java.util.Scanner;
class Pa{
public static void main(String[] args){
Scanner sc = new Scanner (System.in);
String s=sc.nextLine();
char b;
int i,a;
String st="";/to extract a word
s=s+" ";
String t="";/to extract the reverse of the word
a=s.length();
for(i=0;i<a;i++){
b=s.charAt(i);
if(b!=' '){
st=st+b;/word
t=b+st;/reversed word
} else {
if(st.equals(t)){
System.out.println(st);
}
st="";
t="";
}
}
}
}
我無法弄清楚什麼是錯的,也不會欣賞分裂或3個字的迴文查找選項或正常的偶數或奇數迴文查找選項。
http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Biffen
你真的應該打出來迴文邏輯到一個單獨的功能。這將使一切變得更容易調試。創建函數bool isPalindrome(String s)。 – klutt
@Rockabyee Java你在程序的一行中有一個小錯誤。看到我的答案。 –