zoukankan      html  css  js  c++  java
  • Java基础50题test7—处理字符串

    题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
    程序分析:利用 while 语句,条件为输入的字符不为' '.

     1 import java.util.*;
     2 public class Test7 {
     3 
     4     public static void main(String[] args) {
     5         System.out.println("请输入一段字符串:");
     6         Scanner str = new Scanner(System.in);
     7         String s = str.nextLine();
     8         str.close();
     9         char zfs[] = s.toCharArray();
    10         int num=0;
    11         int zifu=0;
    12         int space=0;
    13         int other=0;
    14         for(int i=0;i<s.length();i++)
    15         {
    16             if(Character.isDigit(zfs[i]))
    17                 num++;
    18             else if(Character.isLetter(zfs[i]))
    19                 zifu++;
    20             else if(Character.isSpace(zfs[i]))
    21                 space++;
    22             else
    23                 other++;
    24         }
    25         System.out.println("字符串中数字个数为:"+num);
    26         System.out.println("字符串中字母个数为:"+zifu);
    27         System.out.println("字符串中空格个数为:"+space);
    28         System.out.println("字符串中其他字符个数为:"+other);
    29     }
  • 相关阅读:
    Jedis API操作Redis数据库
    Go开发环境安装与环境变量配置
    Java中使用md5进行hash运算
    oracle创建表空间、用户
    CentOS安装MySQL
    Go语言之快速排序
    软件包管理rpm和yum
    第十一节:configParse模块
    redis数据库
    tcpdump命令
  • 原文地址:https://www.cnblogs.com/zynevergiveup12/p/11145373.html
Copyright © 2011-2022 走看看