找出字符串中所有偶数的个数

输入一串字符串(只包含数字),统计该字符串中所有偶数的个数。

public class Day1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String word = scan.next();
        int flag = 0;
        for(int i=0;i<word.length();i++){
            if((word.charAt(i)+'0')%2==0){
                flag++;
            }
        }
        System.out.println(flag);
    }
}

输出结果:

1112111211121
3

如果求奇数个数,则:

public class Day1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String word = scan.next();
        int flag = 0;
        for(int i=0;i<word.length();i++){
            if((word.charAt(i)+'0')%2!=0){
                flag++;
            }
        }
        System.out.println(flag);
    }
}

输出结果:

23564911525446
7

相关推荐

  1. 字符串所有偶数个数

    2024-04-05 11:28:02       36 阅读
  2. 字符串第一个匹配项下标

    2024-04-05 11:28:02       58 阅读
  3. SCAU:18043 3个数最大

    2024-04-05 11:28:02       55 阅读
  4. LeetCode 28.字符串第一个匹配项下标

    2024-04-05 11:28:02       65 阅读
  5. leetcode28. 字符串第一个匹配项下标

    2024-04-05 11:28:02       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 11:28:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 11:28:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 11:28:02       82 阅读
  4. Python语言-面向对象

    2024-04-05 11:28:02       91 阅读

热门阅读

  1. 单例模式的多种写法

    2024-04-05 11:28:02       39 阅读
  2. 设计模式:单例模式六种实现

    2024-04-05 11:28:02       36 阅读
  3. 单例模式详解

    2024-04-05 11:28:02       32 阅读
  4. Visual Studio Code(VS Code)安装教程

    2024-04-05 11:28:02       29 阅读
  5. Vue 组件的 mixin 函数,用于屏幕适配

    2024-04-05 11:28:02       38 阅读
  6. 0基础如何进入IT行业

    2024-04-05 11:28:02       33 阅读
  7. C/C++中的static关键字用法总结

    2024-04-05 11:28:02       39 阅读
  8. 梯度反向传播过程是如何处理repeat函数的

    2024-04-05 11:28:02       39 阅读
  9. linux小工具杂记

    2024-04-05 11:28:02       35 阅读
  10. 算法思想 - 贪心算法

    2024-04-05 11:28:02       38 阅读
  11. vue3从精通到入门12:vue3的生命周期和组件

    2024-04-05 11:28:02       40 阅读
  12. 英语写作中“概念”concept 、notion、idea的用法

    2024-04-05 11:28:02       75 阅读