String str = ""; System.out.println("请输入一个字符串"); Scanner input = new Scanner(System.in); str = input.next(); StringBuffer sb = new StringBuffer(str); sb.reverse();// 将Str中的字符串倒置 int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == sb.charAt(i)) { count++; } } if (count == str.length()) { System.out.println("此字符串是一个回文字符串"); } else { System.out.println("此字符串不是一个回文字符串"); }
java小练习:回文字符的判断(从前往后、从后往前读都一样)
于 2023-11-15 17:00:31 首次发布