zoukankan      html  css  js  c++  java
  • 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数

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

    程序分析:利用while语句,条件为输入的字符不为 ' '. 

     1 package com.li.FiftyAlgorthm;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 
     7  * 
     8  * 程序分析:利用while语句,条件为输入的字符不为 '
     '
     9  * @author yejin
    10  */
    11 public class CharacterStatistics {
    12     static int digital = 0;
    13     static int character = 0;
    14     static int other = 0;
    15     static int blank = 0;
    16 
    17     public static void main(String[] args) {
    18         char[] ch = null;
    19         Scanner sc = new Scanner(System.in);
    20         String s = sc.nextLine();
    21         ch = s.toCharArray();
    22 
    23         for (int i = 0; i < ch.length; i++) {
    24             if (ch[i] >= '0' && ch[i] <= '9') {
    25                 digital++;
    26             } else if ((ch[i] >= 'a' && ch[i] <= 'z') || ch[i] > 'A'
    27                     && ch[i] <= 'Z') {
    28                 character++;
    29             } else if (ch[i] == ' ') {
    30                 blank++;
    31             } else {
    32                 other++;
    33             }
    34 
    35         }
    36         System.out.println("数字个数: " + digital);
    37         System.out.println("英文字母个数: " + character);
    38         System.out.println("空格个数: " + blank);
    39         System.out.println("其他字符个数:" + other);
    40     }
    41 }
  • 相关阅读:
    P3158 [CQOI2011]放棋子
    Codeforces 1220D. Alex and Julian
    Codeforces 1220C. Substring Game in the Lesson
    Codeforces 1220B. Multiplication Table
    BZOJ 3260. 跳
    BZOJ 3251. 树上三角形
    P1398 [NOI2013]书法家
    P1224 [NOI2013]向量内积
    The Preliminary Contest for ICPC Asia Shanghai 2019 A. Lightning Routing I
    P4271 [USACO18FEB]New Barns
  • 原文地址:https://www.cnblogs.com/justdoitba/p/7142719.html
Copyright © 2011-2022 走看看