欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java练习,

来源: javaer 分享于  点击 18784 次 点评:223

Java练习,


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

import java.util.Scanner;

public class T10 {
    public static void main(String[] args) {
        int digital = 0;
        int character = 0;
        int other = 0;
        int blank = 0;
        char[] ch = null; 
        Scanner in = new Scanner(System.in);
        String str = new String();
        str = in.nextLine();   
        ch = str.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if(ch[i]>='0'&&ch[i]<='9'){
                digital++;
            }else if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z')) {
                character++;
            }else if (ch[i]==' ') {
                blank++;
            }else {
                other++;
            }
        }
        System.out.println("数字:"+digital);
        System.out.println("字母:"+character);
        System.out.println("空格:"+blank);
        System.out.println("其他:"+other);
    }
}

相关文章

    暂无相关文章
相关栏目:

用户点评