2017-03-23 69 views
0
package Checkers; 
import java.util.Scanner; 
public class iterative 
{ 
    public static void main(String args[]) 
    { 
    String reverseString=""; 
    Scanner scanner = new Scanner(System.in); 
    String more = null; 
    do { 
    System.out.println("Enter a string to check if it is a palindrome:"); 
    String inputString = scanner.nextLine(); 
    String combined = inputString.replaceAll("[\\W]", ""); 
    combined = combined.toLowerCase(); 

    int length = combined.length(); 

    for (int i = length - 1 ; i >= 0 ; i--) 
    reverseString = reverseString + combined.charAt(i); 

    if (combined.equals(reverseString)) 
    System.out.println("Input string is a palindrome."); 
    else 
    System.out.println("Input string is not a palindrome."); 
    // Ask user to Evaluate another string 
    System.out.println(); 
    System.out.print("Evaluate another string? (Y=Yes): "); 
    more = scanner.nextLine(); 
    System.out.println(); 
    }  
    while (more.equalsIgnoreCase("y")); 
} 

} 

輸出:輸入字符串以檢查它是否是一個迴文: 雷達 輸入的字符串迴文。迴文重複檢查

評估另一個字符串? (Y =是):y

輸入一個字符串以檢查它是否爲迴文: radar 輸入字符串不是迴文。

評估另一個字符串? (是=是):

我有這個問題,當我把迴文放在像雷達一樣。它是一個迴文。再次放入,我得到它不是一個迴文。我沒有什麼錯,請幫助

+0

reverseString未復位檢查第二輸入。 – jlordo

+0

因爲'reverseString'在你的循環之外,你不會重置它 –

回答

0

放線

String reverseString=""; 

do { ...} 
+0

謝謝你的工作 –