Java 实例 使用Stack,Queue,for或while循环检查回文串

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

1)使用堆栈

2)使用队列

3)使用for/while循环

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

import java.util.Stack;
import java.util.Scanner;
class PalindromeTest {

    public static void main(String[] args) {

        System.out.print("Enter any string:");
        Scanner in=new Scanner(System.in);
        String inputString = in.nextLine();
        Stack stack = new Stack();

        for (int i = 0; i < inputString.length(); i++) {
            stack.push(inputString.charAt(i));
        }

        String reverseString = "";

        while (!stack.isEmpty()) {
            reverseString = reverseString+stack.pop();
        }

        if (inputString.equals(reverseString))
            System.out.println("The input String is a palindrome.");
        else
            System.out.println("The input String is not a palindrome.");

    }
}

输出 1:

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

输出 2:

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

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

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.");

    }
}

输出 1:

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

输出 2:

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

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

import java.util.Scanner;
class PalindromeTest {
   public static void main(String args[])
   {
      String reverseString="";
      Scanner scanner = new Scanner(System.in);

      System.out.println("Enter a string to check if it is a palindrome:");
      String inputString = scanner.nextLine();

      int length = inputString.length();

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

      if (inputString.equals(reverseString))
         System.out.println("Input string is a palindrome.");
      else
         System.out.println("Input string is not a palindrome.");

   }
}

输出 1:

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

输出 2:

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

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

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 示例