小编Ano*_*ous的帖子

java中如何计算以大写字母开头的单词?

我想打一个打印的话,该号码的程序启动以大写字母。所以我做了两个字符串str1 = "The deed is done"str2 = "My name is Bond, JAMES Bond"。对于第一个字符串,它打印了我想要的 1。但是对于第二个,它打印 8 而不是 4,因为它JAMES是大写的。


    public static void main(String[] args){
        String str1 = "The deed is done";
        String str2 = "My name is Bond, JAMES Bond";

        System.out.println(uppercase(str2));
    }

    public static int uppercase(String str){
        int cnt = 0;

        for(int i = 0; i < str.length(); i++){
            if(Character.isUpperCase(str.charAt(i)))
                cnt++;
        }

        return cnt;
    }

Run Code Online (Sandbox Code Playgroud)

这就是我到目前为止所拥有的。我如何才能不计算该单词中的其他字母?

java count cpu-word uppercase

1
推荐指数
1
解决办法
97
查看次数

标签 统计

count ×1

cpu-word ×1

java ×1

uppercase ×1