大家看下这个怎么样,欢迎大家提出意见共同改进。
- public class TestNumber {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- System.out.println("请输入:");
- String str = in.nextLine();
- if (isNum(str)) {
- System.out.println("数字");
- } else {
- System.out.println("字符串");
- }
- }
- public static boolean isNum(String str) {
- Pattern p = Pattern.compile("-?[0-9]+.?[0-9]*[^A-Za-z]");
- Matcher m = p.matcher(str);
- if(m.matches()){
- return true;
- }else{
- return false;
- }
- }
- }
复制代码 |