代码如下:
package ClassDemo;
import java.util.Scanner;
public class CheckPalindrome {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String s = input.nextLine();
int low = 0;
int high = s.length()-1;
boolean flag = true;
while (low < high) {
if(s.charAt(low) != s.charAt(high)) {
flag = false;
break;
}
low++;
high--;
}
if (flag) {
System.out.println(s + " 是回文串");
} else {
System.out.println(s + " 不是回文串");
}
}
}
时间: 2024-10-08 21:18:22