测试输入字符串的长度,及各种类型字符的数量

废话不多说,实际就只是我学习中的记录。

下面直接上代码!

 1 import java.util.Scanner;
 2 public class ScannerTest {
 3 
 4     public static void main(String[] args) {
 5         show1();
 6     }
 7     private static void show1(){
 8         int countUpper=0;
 9         int countLower=0;
10         int countNum=0;
11         int countOfter=0;
12         Scanner scanner = new Scanner(System.in);
13         System.out.print("请输入字符串:");
14         String next = scanner.next();
15         System.out.print("字符串长度为:");
16         int num = next.length();
17         System.out.println(num);
18         //转换成byte类型容易比较
19         byte[] bytes = next.getBytes();
20         //测试字符大小
21         //System.out.println(bytes[0]);
22         //循环迭代字符数量
23         for (int i = 0; i <next.length() ; i++) {
24             if(bytes[i]<=58&&bytes[i]>=49){
25                 countNum++;
26             }else if(bytes[i]<=122&&bytes[i]>=97){
27                 countLower++;
28             }else if(bytes[i]<=90&&bytes[i]>=65){
29                 countUpper++;
30             }else{
31                 countOfter++;
32             }
33         }
34         System.out.print("输入的字符串有:\n"+countUpper+"个大写字母!");
35         System.out.print(""+countLower+"个小写字母!");
36         System.out.print(""+countNum+"个数字!");
37         System.out.print(""+countOfter+"个其他字符!");
38     }
39 }

猜你喜欢

转载自www.cnblogs.com/YLTzxzy/p/10901598.html