题目内容:
你的程序要读入一行文本,其中以空格分隔为若干个单词,以‘.’结束。你要输出这行文本中每个单词的长度。这里的单词与语言无关,可以包括各种符号,比如“it’s”算一个单词,长度为4。注意,行中可能出现连续的空格。
输入格式:
输入在一行中给出一行文本,以‘.’结束,结尾的句号不能计算在最后一个单词的长度内。
输出格式:
在一行中输出这行文本对应的单词的长度,每个长度之间以空格隔开,行末没有最后的空格。
输入样例:
It’s great to see you here.
输出样例:
4 5 2 3 3 4
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String word = in.next();
boolean mark = true;
while ( word.indexOf(".") == -1)
{
if (mark)
{
System.out.print( word.length());
mark = false;
}
else
{
System.out.print(" "+word.length());
}
word = in.next();
}
if ( mark )
{
if ( word.length() == 1)
{
System.out.print("");
}
else
{
System.out.print(+(word.length()-1));
}
}
else
{
System.out.print(" "+(word.length()-1));
}
另外在mooc上看到了一种更好的方法。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int count = 0;
int sum = 0;
int blank = 0;
int point = 0;
int last = 0;
for(int i = 0;i < s.length();i++) {
if(s.charAt(i)!=' ')
{
count++;
}else {
if(count!=0) {
System.out.print(count+" ");
}
sum = sum + count;
blank = blank + 1;
count = 0;
}
if(s.charAt(i)=='.') {
point = point + 1;
}
}
last = s.length()-sum-blank-point;
if(last != 0) {
System.out.print(last);
}
}