本题我们使用ASCLL值的方法解决,也可以打表去判断。加深对ASCLL表的认识!
图源ASCII 表 | 菜鸟教程,有需要的小伙伴可以在菜鸟详细了解。
由题知要识别三种类型的字符,使用三个变量存储最终值输出即可。根据ASCLL表可知数字、大写字母、小写字母都是连续的,我们只需要接收输入的值进行判断在那个范围那个变量就++即可
代码实现👇
public static void main(String[] args) {Scanner scanner=new Scanner(System.in);String string=scanner.nextLine();//接受一行字符int num=0;//数字int ba=0;//大写字母int sa=0;//小写字母for (int i = 0; i < string.length(); i++) {if (string.charAt(i)-48>=0 && string.charAt(i)-48<9) {num++;}else if (string.charAt(i)-65>=0 && string.charAt(i)-65<26) {ba++;}else if (string.charAt(i)-97>=0 && string.charAt(i)-97<26) {sa++;}}System.out.println(ba);System.out.println(sa);System.out.println(num);}
通过本章学习一定要牢记掌握数字大写小写ASCLL为48,65,97!!!