3 回答

TA贡献948条经验 获得超1109个赞
package test; public class Test { public static void main(String[] args) { printEachWordLength("It's great to see you here."); System.out.println("\n---------------"); printEachWordLength("It's great to see you here."); } public static void printEachWordLength(String str) { if (str == null) return; if ("".equals(str.trim())) return; String[] words = str.split(" "); for (String word : words) { if ("".equals(word.trim())) continue; int length = word.length(); if (word.endsWith(".")) length--; System.out.print(length+" "); } } }
没有考虑末尾句点前有空格的情况,如果有必要,你自己补充一下

TA贡献3593条经验 获得超0个赞

TA贡献1条经验 获得超0个赞
package 作业1;
public class 作业6 {
public static void main(String[] args) {
printEachWordLength("It's great to see you here.");
System.out.println("\n---------------");
printEachWordLength("It's great to see you here.");
}
public static void printEachWordLength(String str) {
if (str == null) return;
if ("".equals(str.trim())) return;
String[] words = str.split(" ");
for (String word : words) {
if ("".equals(word.trim())) continue;
int length = word.length();
if (word.endsWith(".")) length--;
System.out.print(length+" ");
}
}
}
添加回答
举报