我想知道如何编写一个方法来计算单词的数量和每个单词字母的数量,例如,如果输入是"蓝天"作为回报我带一些东西,告诉我有3个字3个字母4个字母3个字母
我已经找到了这个代码
public static int countWords(String s){
int wordCount = 0;
boolean word = false;
int endOfLine = s.length() - 1;
for (int i = 0; i < s.length(); i++) {
// if the char is a letter, word = true.
if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
word = true;
// if char isn't a letter and there have been letters before,
// counter goes up.
} else if (!Character.isLetter(s.charAt(i)) && word) {
wordCount++;
word = false;
// last …Run Code Online (Sandbox Code Playgroud)