【发布时间】:2019-12-17 21:17:07
【问题描述】:
我想制作一个程序,打印出 START 大写字母的单词数。所以我做了两个字符串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;
}
这就是我目前所拥有的。我怎样才能使该单词中的其他字母不被计算在内?
【问题讨论】: