我想打一个打印的话,该号码的程序启动以大写字母。所以我做了两个字符串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)
这就是我到目前为止所拥有的。我如何才能不计算该单词中的其他字母?