java判断各类型字符个数实例代码

作者:小孙的代码分享 时间:2022-01-22 16:39:15 

描述

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

输入描述:

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

输出描述:

输出字符串中包含的英文字母个数,数字个数,空格个数,其它字符个数(格式为:英文字母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<str.length();i++){
           char c=str.charAt(i);
           if((c>='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);
   }
}

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

来源:https://blog.csdn.net/weixin_53939785/article/details/122188422

标签:java,字符,个数
0
投稿

猜你喜欢

  • Java调用wsdl接口的两种方法(axis和wsimport)

    2023-06-23 14:41:22
  • Android手势识别器GestureDetector使用详解

    2022-01-16 14:25:17
  • 完成OSS.Http底层HttpClient重构封装 支持标准库

    2021-09-28 11:25:40
  • C#实现餐厅管理系统

    2023-11-27 16:05:05
  • 浅析java中的取整(/)和求余(%)

    2023-04-30 23:46:23
  • Spring注解@Configuration和@Component区别详解

    2022-11-05 02:04:18
  • C#简单实现显示中文格式星期几的方法

    2021-09-08 12:27:05
  • 解决JAVA遍历List集合,删除数据时出现的问题

    2021-12-25 15:38:03
  • springboot整合mybatis-plus基于注解实现一对一(一对多)查询功能

    2021-06-16 13:08:29
  • spring mvc中直接注入的HttpServletRequst安全吗

    2021-12-29 07:48:16
  • java文件操作之java写文件简单示例

    2023-10-21 14:37:13
  • 详解Spring框架注解扫描开启之配置细节

    2022-11-01 18:04:14
  • Java动态获取实现某个接口下所有的实现类对象集合

    2023-04-01 14:43:20
  • SpringBoot整合Mybatis,解决TypeAliases配置失败的问题

    2023-11-28 14:59:24
  • Flutter实现用视频背景的登录页的示例代码

    2022-01-02 05:45:54
  • 解析spring事务管理@Transactional为什么要添加rollbackFor=Exception.class

    2021-09-03 17:07:41
  • java实现将数字转换成人民币大写

    2023-08-11 05:07:29
  • SpringMVC数据页响应ModelAndView实现页面跳转

    2022-04-29 15:21:10
  • SpringBoot Data JPA 关联表查询的方法

    2021-08-08 13:59:23
  • Android通过代码控制ListView上下滚动的方法

    2022-06-29 03:07:57
  • asp之家 软件编程 m.aspxhome.com