java_回文检测

 1 package bao;
 2 import java.util.Scanner;
 3 public class Work {
 4 
 5     public static boolean digui(String str1,int i,int j) {
 6         
 7         if(j==1||j==0) {
 8             return true;
 9         }
10         else {
11             if(str1.charAt(i)==str1.charAt(j)) {
12                 return digui(str1,++i,--j);
13             }
14             else {
15                 return false;
16             }
17         }
18             
19     }//end digui
20     public static void main(String[] args) {
21         Scanner sca=new Scanner(System.in);
22         boolean bool;
23         String str;
24         str=sca.next();
25         int i=0;
26         int j=str.length()-1;
27         bool=digui(str,i,j);
28         if(bool==true) {
29             System.out.println(str+" 是回文!");
30         }
31         else {
32             System.out.println(str+" 不是回文");
33         }
34 
35         
36         
37     }//end main
38 }//end Work

回文检测使用的递归的方法对输入的字符串进行判断,若字符串长度等于1或者0返回true,若长度不唯一,则对第一个和最后一个字符判断若相等进行第二个和倒数第二个字符判断,类推;若不相等则返回false。最后根据布尔类型变量是true还是false输出字符串是否是回文。

posted @ 2019-09-25 19:18  dululudululu  阅读(249)  评论(0编辑  收藏  举报