2023-03-07 java 输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数。

本文介绍了如何使用Java编程来统计输入字符串中英文字母、空格、数字以及其它特殊字符的个数,通过示例代码详细解析了统计过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.求出上面数组中0-9分别出现的次数
@Test
public void zuoye01(){
  /*  int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
    求出上面数组中0-9分别出现的次数
            双重for循环*/
    int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};

    for(int num = 0; num <= 9; num++){  //数字是从0-9
        int count01 = 0;
        for(int i = 0; i <= scores.length - 1; i++){
            if(num == scores[i]){   //运行300次
                count01++;
            }
        }
        System.out.println(num+":  "+count01);
    }
}
 @Test
    public void zuoye01(){
      /*  int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        求出上面数组中0-9分别出现的次数
                双重for循环*/
        int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};

        for(int num = 0; num <= 9; num++){  //数字是从0-9
            int count01 = 0;
            for(int i = 0; i <= scores.length - 1; i++){
                if(num == scores[i]){   //运行300次
                    count01++;
                }
            }
            System.out.println(num+":  "+count01);
        }
    }
2.改进版,用十个计数器,这样统计一遍就行

@Test
public void zuoye0101(){
  /*  int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
    求出上面数组中0-9分别出现的次数
            双重for循环*/
    int[] array={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
    int[] countarray = new int[10]; //弄十个计数器,这样统计一遍就可以把个数统计完全
    for (int i = 0;i < array.length;i++) {
        countarray[array[i]]++; //走一遍就可以
    }
    for (int i = 0; i < 10;i++) {
        System.out.println(i+"的个数为:"+countarray[i]);
    }
}
 
 @Test
    public void zuoye0101(){
      /*  int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        求出上面数组中0-9分别出现的次数
                双重for循环*/
        int[] array={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9};
        int[] countarray = new int[10]; //弄十个计数器,这样统计一遍就可以把个数统计完全
        for (int i = 0;i < array.length;i++) {
            countarray[array[i]]++; //走一遍就可以
        }
        for (int i = 0; i < 10;i++) {
            System.out.println(i+"的个数为:"+countarray[i]);
        }
    }


3.输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数。
@Test
public void zuoye18(){
    System.out.println("请输入字符串:");
    Scanner scanner = new Scanner(System.in);
    String string01 = scanner.nextLine();   //遇到回车就结束,可以接收空格
  //  String string01 = scanner.next();   //这个.next()   遇到空格或者是回车就结束了
    System.out.println(string01);
    char[] array = string01.toCharArray();//变成字符数组
    int lettercount = 0;
    int spacecount = 0;
    int numbercount = 0;
    int othercount = 0;

    for (int i = 0; i < array.length ; i++) {
            if((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z')){
                lettercount++;
            }else if(array[i] == ' ' ){
                spacecount++;
            }else if(array[i] >= '0' && array[i] <= '9'){
                numbercount++;
            }else{
                othercount++;
            }
    }
    System.out.println("英文字母:"+lettercount);
    System.out.println("空格:"+spacecount);
    System.out.println("数字:"+numbercount);
    System.out.println("其他:"+othercount);
    
}

 @Test
    public void zuoye18(){
        System.out.println("请输入字符串:");
        Scanner scanner = new Scanner(System.in);
        String string01 = scanner.nextLine();   //遇到回车就结束,可以接收空格
      //  String string01 = scanner.next();   //这个.next()   遇到空格或者是回车就结束了
        System.out.println(string01);
        char[] array = string01.toCharArray();//变成字符数组
        int lettercount = 0;
        int spacecount = 0;
        int numbercount = 0;
        int othercount = 0;

        for (int i = 0; i < array.length ; i++) {
                if((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z')){
                    lettercount++;
                }else if(array[i] == ' ' ){
                    spacecount++;
                }else if(array[i] >= '0' && array[i] <= '9'){
                    numbercount++;
                }else{
                    othercount++;
                }
        }
        System.out.println("英文字母:"+lettercount);
        System.out.println("空格:"+spacecount);
        System.out.println("数字:"+numbercount);
        System.out.println("其他:"+othercount);
        
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值