编写程序判断数字字符串是否为对称数
(如: 12321、2334、2345432为对称数,从左往右或从右往左看为相同数字)
public class DuiChenShu { public static void main(String[] args) { System.out.println(isDuiChenShu("12321")); System.out.println(isDuiChenShu("2332")); System.out.println(isDuiChenShu("234432")); System.out.println(isDuiChenShu("2344432")); System.out.println(isDuiChenShu("2345632")); System.out.println(isDuiChenShu("34563")); } public static boolean isDuiChenShu(String strInt) { int length = strInt.length(); // System.out.println("length:" + length); if (length % 2 == 0) { // System.out.println("length % 2 == 0, strInt:" + strInt); // System.out.println("length/2=" + length / 2); for (int i = 0; i < length / 2; i++) { // System.out.println(strInt.charAt(i) + " vs " + strInt.charAt(length - 1 - i)); if (!(strInt.charAt(i) == strInt.charAt(length - 1 - i))) return false; } } else { // System.out.println("length % 2 <> 0, strInt:" + strInt); // System.out.println("(length -1)/2=" + (length - 1) / 2); for (int i = 0; i < (length - 1) / 2; i++) { // System.out.println(strInt.charAt(i) + " vs " + strInt.charAt(length - 1 - i)); if (!(strInt.charAt(i) == strInt.charAt(length - 1 - i))) return false; } } return true; } }