zoukankan      html  css  js  c++  java
  • Java经典编程题50道之七

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

    public class Example07 {
        public static void main(String[] args) {
            String s = "Hello World! BeiJing AoYun 2008。";
            number(s);
        }

        public static void number(String s) {
            int digital = 0;
            int character = 0;
            int other = 0;
            int blank = 0;
            char[] ch = s.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("要测试的字符串为: " + s);
            System.out.println("数字个数: " + digital + " 英文字母个数: " + character
                    + " 空格个数: " + blank + " 其他字符个数:" + other);
        }
    }

  • 相关阅读:
    欢迎使用CSDN-markdown编辑器
    欢迎使用CSDN-markdown编辑器
    Math类简介
    Math类简介
    http_server
    tcp服务器
    swoole安装
    laravel源码解析
    VMware的Unity模式
    string.format() %d越界的问题
  • 原文地址:https://www.cnblogs.com/qubo520/p/6928062.html
Copyright © 2011-2022 走看看