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

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

下面直接上代码!

 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 }

 

转载于:https://www.cnblogs.com/YLTzxzy/p/10901598.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值