java统计输入的字符串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cheng_May/article/details/81076231

   根据Ascll码表来进行统计数据:

System.out.println("请输入字符串:");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int bigchar = 0,smallchar=0,number = 0,blank = 0,other=0;
        int nine=0,one=0,three=0,five=0,serven=0;
        int n = str.length();
        
        for(int i=0;i<n;i++){
            if(str.charAt(i) >=65&&str.charAt(i)<=90){
                bigchar++;
                
            }else if(str.charAt(i)>=97&&str.charAt(i)<=122){
                smallchar++;
            }else if(str.charAt(i)>=48&&str.charAt(i)<=57){
                if(str.charAt(i) == 49){
                    one++;
                }else if(str.charAt(i)== 51){
                    three++;
                }else if(str.charAt(i)==53){
                    five++;
                }else if(str.charAt(i)== 55){
                    serven++;
                }else if(str.charAt(i)==57){
                    nine++;
                }
                
                number++;
            }else if(str.charAt(i)==32){
                blank++;
            }else {
                other++;
            }        
        }
        System.out.println("大写字母个数:"+bigchar);
        System.out.println("小写字母个数:"+smallchar);
        System.out.println("数字个数:"+number);
        System.out.println("空格个数:"+blank);
        System.out.println("其他字母个数:"+other);
        System.out.println("1出现的个数:"+one);
        System.out.println("3出现的个数:"+three);
        System.out.println("5出现的个数:"+five);
        System.out.println("7出现的个数:"+serven);
        System.out.println("9出现的个数:"+nine);
        System.out.println("13579的总个数:"+(one+three+five+serven+nine));
        
    }

运行结果图:

猜你喜欢

转载自blog.csdn.net/Cheng_May/article/details/81076231