原文: https://beginnersbook.com/2014/01/java-program-to-check-palindrome-string/

    在本教程中,我们将看到程序来检查给定的String是否是回文。以下是实现目标的方法。

    1)使用堆栈

    2)使用队列

    3)使用for/while循环

    程序 1:使用堆栈进行回文检查

    1. import java.util.Stack;
    2. import java.util.Scanner;
    3. class PalindromeTest {
    4. public static void main(String[] args) {
    5. System.out.print("Enter any string:");
    6. Scanner in=new Scanner(System.in);
    7. String inputString = in.nextLine();
    8. Stack stack = new Stack();
    9. for (int i = 0; i < inputString.length(); i++) {
    10. stack.push(inputString.charAt(i));
    11. }
    12. String reverseString = "";
    13. while (!stack.isEmpty()) {
    14. reverseString = reverseString+stack.pop();
    15. }
    16. if (inputString.equals(reverseString))
    17. System.out.println("The input String is a palindrome.");
    18. else
    19. System.out.println("The input String is not a palindrome.");
    20. }
    21. }

    输出 1:

    1. Enter any string:abccba
    2. The input String is a palindrome.

    输出 2:

    1. Enter any string:abcdef
    2. The input String is not a palindrome.

    程序 2:回顾检查使用队列

    1. import java.util.Queue;
    2. import java.util.Scanner;
    3. import java.util.LinkedList;
    4. class PalindromeTest {
    5. public static void main(String[] args) {
    6. System.out.print("Enter any string:");
    7. Scanner in=new Scanner(System.in);
    8. String inputString = in.nextLine();
    9. Queue queue = new LinkedList();
    10. for (int i = inputString.length()-1; i >=0; i--) {
    11. queue.add(inputString.charAt(i));
    12. }
    13. String reverseString = "";
    14. while (!queue.isEmpty()) {
    15. reverseString = reverseString+queue.remove();
    16. }
    17. if (inputString.equals(reverseString))
    18. System.out.println("The input String is a palindrome.");
    19. else
    20. System.out.println("The input String is not a palindrome.");
    21. }
    22. }

    输出 1:

    1. Enter any string:xyzzyx
    2. xyzzyx
    3. The input String is a palindrome.

    输出 2:

    1. Enter any string:xyz
    2. The input String is not a palindrome.

    程序 3:使用for循环/While循环和字符串函数charAt

    1. import java.util.Scanner;
    2. class PalindromeTest {
    3. public static void main(String args[])
    4. {
    5. String reverseString="";
    6. Scanner scanner = new Scanner(System.in);
    7. System.out.println("Enter a string to check if it is a palindrome:");
    8. String inputString = scanner.nextLine();
    9. int length = inputString.length();
    10. for ( int i = length - 1 ; i >= 0 ; i-- )
    11. reverseString = reverseString + inputString.charAt(i);
    12. if (inputString.equals(reverseString))
    13. System.out.println("Input string is a palindrome.");
    14. else
    15. System.out.println("Input string is not a palindrome.");
    16. }
    17. }

    输出 1:

    1. Enter a string to check if it is a palindrome:
    2. aabbaa
    3. Input string is a palindrome.

    输出 2:

    1. Enter a string to check if it is a palindrome:
    2. aaabbb
    3. Input string is not a palindrome.

    如果你想在上面的程序中使用While循环,那么用这段代码替换for循环:

    1. int i = length-1;
    2. while ( i >= 0){
    3. reverseString = reverseString + inputString.charAt(i);
    4. i--;
    5. }