Palindrome迭代检查器

时间:2017-03-23 23:25:33

标签: java

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

输入一个字符串以检查它是否是回文: 雷达 输入字符串不是回文。

评估另一个字符串? (Y =是):

当我把雷利放在像雷达这样的回文中时,我遇到了这个问题。它是一个回文。把它放进去,我得到它不是回文。我没有错,请帮忙

1 个答案:

答案 0 :(得分:0)

放线

String reverseString="";

do { ...}