源代码
package test;
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
System.out.println("请输入判断的字符串");
Scanner scan=new Scanner(System.in);
String str=scan.next();
int n = str.length();
boolean flag=dg(str,0);
if(flag) {
System.out.println("该字符串是回文");
}
else {
System.out.println("该字符串不是回文");
}
}
public static boolean dg(String str,int i) {
int n=str.length();
if(n-i==0||n-i==1) {
return true;
}
else {
if(str.charAt(i)==str.charAt(n-i-1)) {
return dg(str,i+1);
}
return false;
}
}
}
原文地址:https://www.cnblogs.com/xhj1074376195/p/11580747.html
时间: 2024-11-05 21:37:09