/** * 判断一个数字或字符串是不是回文的; * */ public class HuiWenShu { public static void main(String[] args) { System.out.println(HuiWenShu.isHuiWenShu(12321)); System.out.println(HuiWenShu.isHuiWenShu(String.valueOf(12321))); System.out.println(HuiWenShu.isHuiWenShu(";abcba;")); } //判断字符串是不是回文的 private static boolean isHuiWenShu(String str) { StringBuffer stringBuffer = new StringBuffer(str); stringBuffer.reverse(); return stringBuffer.toString().equals(str); } //判断数字是不是回文的 private static boolean isHuiWenShu(int num) { int oldValue = num; int temp = 0; //把数字倒置 while (num > 0) { temp = temp * 10 + num % 10; num /= 10; } return temp == oldValue; } }