运用了布尔类型进行判断,递归调用了isHuiWen函数。
package ketangceshia;
import java.util.Scanner;
public class HuiWendg {
public static void main( String args[] )
{
Scanner scan=new Scanner(System.in);
String a=scan.next();
int n=a.length();
if(a.length()==0)//字符串长度为0时
System.out.println("是回文!");
else
{
boolean b=isHuiWen(a,0,n);
if(b==true)
System.out.println("是回文!");
else
if(b==false)
System.out.println("不是回文!");
}
}
private static boolean isHuiWen(String str, int start, int length) {
if(start==length)
return true;
else if(str.toCharArray()[start]==str.toCharArray()[length-1]){
return isHuiWen(str,start+1,length-1);
}
return false;
}
}
public static void main( String args[] )
{
Scanner scan=new Scanner(System.in);
String a=scan.next();
int n=a.length();
if(a.length()==0)//字符串长度为0时
System.out.println("是回文!");
else
{
boolean b=isHuiWen(a,0,n);
if(b==true)
System.out.println("是回文!");
else
if(b==false)
System.out.println("不是回文!");
}
}
private static boolean isHuiWen(String str, int start, int length) {
if(start==length)
return true;
else if(str.toCharArray()[start]==str.toCharArray()[length-1]){
return isHuiWen(str,start+1,length-1);
}
return false;
}
}