首页 > 编程语言 > 详细

java回文串

时间:2019-09-25 19:58:39      阅读:93      评论:0      收藏:0      [点我收藏+]

  用递归写函数判断回文串,当长度为1或0时输出是回文串结束,当长度大于1时开始递归。

 1 package Zcf;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Statistics {
 6     public static void main(String[] args) {
 7     System.out.println("请输入字符串:");
 8     Scanner in = new Scanner(System.in);
 9     String s=in.next();
10     cuan(s,0);
11     }
12     public static void cuan (String a,int i) {
13         //String ss=a;
14         int len=a.length();
15         len-=i;
16         if(len==1||len==0)
17         {
18             System.out.println("是回文串!");
19             return ;
20         }
21         else
22         {
23             char q=a.charAt(i);
24             char w=a.charAt(len-1);
25             if(q==w)
26             {
27                 cuan(a,++i);
28             }
29             else
30             {
31                 System.out.println("不是回文串!");
32                 return ;
33             }
34             
35         }
36     }
37 }

 

java回文串

原文:https://www.cnblogs.com/125418a/p/11586760.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!