package Test; import java.util.Scanner; public class Test { public static void main(String[] args) { System.out.println("请输入字符串:"); Scanner input=new Scanner(System.in); String str=""; str=input.next(); StringBuffer a=new StringBuffer(str); a.reverse(); int count=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)==a.charAt(i)) { count++; } } if(count==str.length()) { System.out.println(str+"是回文字符串"); } else { System.out.println(str+"不是回文字符串"); } } }
以上为源代码
运行结果截图
编程总结分析
总的来说就是先分析用什么方法,然后一步步去实现。我是先将原本的字符串反转,然后使用递归的方法,挨个判断相对应的位置上字符是否相同,以此判断字符串是否回文