import java.util.Queue;
import java.util.Scanner;
import java.util.LinkedList;
class PalindromeTest
{
public static void main(String[] args)
{
System.out.print("Enter any string:");
Scanner in = new Scanner(System.in);
String inputString = in.nextLine();
Queue queue = new LinkedList();
for (int i = inputString.length()-1; i >=0; i--)
{
queue.add(inputString.charAt(i));
}
String reverseString = "";
while (!queue.isEmpty())
{
reverseString = reverseString+queue.remove();
}
if (inputString.equals(reverseString))
System.out.println("The input String is a palindrome.");
else
System.out.println("The input String is not a palindrome.");
}
}
因此,如果字符串输入是Palindrome,则此代码打印输出,但不确定如何使其成为循环并在输入空行时终止。我尝试过while while循环,但没有运气
答案 0 :(得分:0)
喜欢这样吗?
System.out.print("Enter any string:");
Scanner in = new Scanner(System.in);
String inputString; //extract the inputString as a variable
while (!(inputString = in.nextLine()).equals("")) //just add this loop, the rest is same as yours
{
Queue queue = new LinkedList();
for (int i = inputString.length() - 1; i >= 0; i--)
{
queue.add(inputString.charAt(i));
}
String reverseString = "";
while (!queue.isEmpty())
{
reverseString = reverseString + queue.remove();
}
if (inputString.equalsIgnoreCase(reverseString))
{
System.out.println("The input String is a palindrome.");
}
else
{
System.out.println("The input String is not a palindrome.");
}
System.out.print("Enter any string:");
}