我想檢查是否一個字符串,輸入的字符串,該字符串是迴文檢查迴文(JAVA)(新秀狀態)
,如果它是顯示一些積極 如果不.. (無效)
我目前得到的答案無效(無論輸入什麼內容) 我不太確定如果for循環或布爾語句有問題。
//ACTION FROM BUTTON PERFORMED HERE
private void ButtonActionPerformed(ActionEvent evt) {
//creating variables
String myString = textField1.getText();
int stringLength = myString.length();
char arrayOne[] = new char[stringLength];
char arrayTwo[] = new char[stringLength];
boolean palindrome = false;
//for loop to setup ARRAY ONE
for(int i = 0; i < stringLength-1; i++){
arrayOne[i] = myString.charAt(i);
}
//for loop to setup ARRAY TWO
for(int i = stringLength-1; stringLength-1 > i; i--){
arrayTwo[i] = myString.charAt(i);
}
//for loop checking if array indexes are equivalent in value (char)
for(int i = 0; i < stringLength-1; i++){
if(arrayOne[i] != arrayTwo[i]){
palindrome = false;
}
else{
palindrome = true;
}
}
//assigning text to the text boxes based on boolean palindrome
if(palindrome == true){
textField2.setText("Valid");
}
if(palindrome ==false){
textField2.setText("Invalid");
}
}
}
我想我評論它descently
可以使用調試器或添加調試代碼。 – 2013-02-22 06:17:09
http://stackoverflow.com/questions/4138827/check-string-for-palindrome?rq=1 – 2013-02-22 06:29:22