判断各类型字符个数

导读:本篇文章讲解 判断各类型字符个数,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

描述

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

输入描述:

控制台随机输入一串字符串

输出描述:

输出字符串中包含的英文字母个数,数字个数,空格个数,其它字符个数(格式为:英文字母x数字x空格x其他x),预设代码中已给出输出.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int numbers = 0;
        int words = 0;
        int space = 0;
        int other = 0;
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();

        //write your code here......
        

        System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
    }
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int numbers = 0;
        int words = 0;
        int space = 0;
        int other = 0;
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();

        //write your code here......
        for(int i=0;i<str.length();i++){
            char c=str.charAt(i);
            if((c>='a' && c<='z')|| (c>='A' && c<='Z')){
                words++;
                continue;
            }
            if(c>='0' && c<='9' ){
                numbers++;
                continue;
            }
            if(c==' '){
                space++;
                continue;
            }
            else{
                other++;
                continue;
            }
        }

        System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
    }
}


注意:每次计数完后要跳出循环,否则就取出的字符会挨个问一遍判断语句 出现问题 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/119630.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!