栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

判断各类型字符个数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

判断各类型字符个数

描述

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

输入描述:

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

输出描述:

输出字符串中包含的英文字母个数,数字个数,空格个数,其它字符个数(格式为:英文字母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='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);
    }
}


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/680346.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号