Java判断任意一个数是否是回文数的2种简单写法

第一种:把将要判断的数从尾部逆序依次取出并形成一个新的字符串,用新的字符串与原字符串进行比对即可。

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String s1 = "";
        for (int i = s.length()-1;i >= 0;i--){
            s1 += s.charAt(i);
        }
        System.out.println(s.equals(s1));
    }
}

第二种:利用StringBuilder的reverse()函数,该函数可直接将StringBuilder对象逆序。我们先将原StringBuilder对象转换成字符串1,再调用reverse()函数将新StringBuilder对象转换成字符串2,比较字符串1和2即可。

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder s = new StringBuilder(sc.next());
        String a = String.valueOf(s);
        String b = String.valueOf(s.reverse());
        System.out.println(a.equals(b));
    }
}
public class CharProcessor { // 私有构造方法,防止实例化 private CharProcessor() { throw new AssertionError("不能实例化工具类"); } // 计算字符串长度 public static int getLength(String text) { if (text == null) { return 0; } return text.length(); } // 判断是否包含大写字母 public static boolean hasUppercase(String text) { if (text == null) { return false; } for (char c : text.toCharArray()) { if (Character.isUpperCase(c)) { return true; } } return false; } // 判断是否包含小写字母 public static boolean hasLowercase(String text) { if (text == null) { return false; } for (char c : text.toCharArray()) { if (Character.isLowerCase(c)) { return true; } } return false; } // 判断是否包含字 public static boolean hasDigit(String text) { if (text == null) { return false; } for (char c : text.toCharArray()) { if (Character.isDigit(c)) { return true; } } return false; } // 判断是否包含特殊字符 public static boolean hasSpecialChar(String text) { if (text == null) { return false; } String specialChars = "!@#$%^&*()_+-=[]{}|;:,.<>?/"; for (char c : text.toCharArray()) { if (specialChars.indexOf(c) != -1) { return true; } } return false; } // 计算单词量 public static int countWords(String text) { if (text == null || text.trim().isEmpty()) { return 0; } String[] words = text.trim().split("\\s+"); return words.length; } // 反转字符串 public static String reverseString(String text) { if (text == null) { return null; } return new StringBuilder(text).reverse().toString(); } // 判断是否是回文字符串 public static boolean isPalindrome(String text) { if (text == null) { return false; } String cleaned = text.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); return cleaned.equals(reverseString(cleaned)); } // 统计字符出现次 public static int countChar(String text, char target) { if (text == null) { return 0; } int count = 0; for (char c : text.toCharArray()) { if (c == target) { count++; } } return count; } // 转换为驼峰命名 public static String toCamelCase(String text) { if (text == null || text.isEmpty()) { return text; } String[] words = text.split("[\\s_-]+"); StringBuilder result = new StringBuilder(words[0].toLowerCase()); for (int i = 1; i < words.length; i++) { if (!words[i].isEmpty()) { result.append(Character.toUpperCase(words[i].charAt(0))) .append(words[i].substring(1).toLowerCase()); } } return result.toString(); } // 判断字符串是否为空或null public static boolean isNullOrEmpty(String text) { return text == null || text.trim().isEmpty(); } // 去除所有空格 public static String removeAllSpaces(String text) { if (text == null) { return null; } return text.replaceAll("\\s+", ""); } // 统计大写字母量 public static int countUppercase(String text) { if (text == null) { return 0; } int count = 0; for (char c : text.toCharArray()) { if (Character.isUpperCase(c)) { count++; } } return count; } // 统计小写字母量 public static int countLowercase(String text) { if (text == null) { return 0; } int count = 0; for (char c : text.toCharArray()) { if (Character.isLowerCase(c)) { count++; } } return count; } } // 测试类 class CharProcessorTest { public static void main(String[] args) { String testString1 = "Hello World 123!"; String testString2 = "A man a plan a canal Panama"; String testString3 = "hello-world_example"; String testString4 = "上海自来水来自海上"; System.out.println("测试字符串1: " + testString1); System.out.println("长度: " + CharProcessor.getLength(testString1)); System.out.println("包含大写字母: " + CharProcessor.hasUppercase(testString1)); System.out.println("包含字: " + CharProcessor.hasDigit(testString1)); System.out.println("包含特殊字符: " + CharProcessor.hasSpecialChar(testString1)); System.out.println("单词量: " + CharProcessor.countWords(testString1)); System.out.println("反转: " + CharProcessor.reverseString(testString1)); System.out.println("大写字母量: " + CharProcessor.countUppercase(testString1)); System.out.println("小写字母量: " + CharProcessor.countLowercase(testString1)); System.out.println(); System.out.println("测试字符串2: " + testString2); System.out.println("是否是回文: " + CharProcessor.isPalindrome(testString2)); System.out.println("'a'出现次: " + CharProcessor.countChar(testString2.toLowerCase(), 'a')); System.out.println(); System.out.println("测试字符串3: " + testString3); System.out.println("驼峰命名: " + CharProcessor.toCamelCase(testString3)); System.out.println("去除空格: " + CharProcessor.removeAllSpaces(" hello world ")); System.out.println(); System.out.println("测试字符串4: " + testString4); System.out.println("是否是回文: " + CharProcessor.isPalindrome(testString4)); } }逐行解析代码的功能
最新发布
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值