java用ASCII表知识 如果输入的字符串中有数字 再判断大写字母有多少个 否则输入错误

if()里写什么? 0-99吗?
2025-03-22 15:59:29
推荐回答(1个)
回答1:

public static void checkWord() { //接受输入 Scanner scanner = new Scanner(System.in); //输入的字符串 String str = scanner.nextLine().trim(); //如果字符串长度大于1 if (str.length() > 1) { //不符合要求,提示 System.out.println("输入字符串"); } //如果字符为a到z中间,提示小写字母 else if (str.matches("[a-z]")) { System.out.println("输入小写字母"); } //如果字符为A到Z中间,提示大写字母 else if (str.matches("[A-Z]")) { System.out.println("输入大写字母"); } //如果字符为0到9中间,提示数字 else if (str.matches("[0-9]")) { System.out.println("输入数字"); } //输入的字符不合法,非数字和字母 else { System.out.println("输入既字符串写字母数字"); }}