可以使用Java内置的Character类的isDigit()方法来判断一个字符是否为数字。

示例代码:

public class Main {
    public static void main(String[] args) {
        String str = "12345";
        boolean isNumber = true;
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                isNumber = false;
                break;
            }
        }
        if (isNumber) {
            System.out.println(str + " is a number.");
        } else {
            System.out.println(str + " is not a number.");
        }
    }
}

输出结果:

12345 is a number.
``
用java写程序判断是否为数字

原文地址: https://www.cveoy.top/t/topic/hd7B 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录