📄 countwords.java
字号:
//********************************************************************
// CountWords.java Author: Lewis/Loftus
//
// Demonstrates the use of the StringTokenizer class and nested
// loops.
//********************************************************************
import cs1.Keyboard;
import java.util.StringTokenizer;
public class CountWords
{
//-----------------------------------------------------------------
// Reads several lines of text, counting the number of words
// and the number of non-space characters.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int wordCount = 0, characterCount = 0;
String line, word;
StringTokenizer tokenizer;
System.out.println ("Please enter text (type DONE to quit):");
line = Keyboard.readString();
while (!line.equals("DONE"))
{
tokenizer = new StringTokenizer (line);
while (tokenizer.hasMoreTokens())
{
word = tokenizer.nextToken();
wordCount++;
characterCount += word.length();
}
line = Keyboard.readString();
}
System.out.println ("Number of words: " + wordCount);
System.out.println ("Number of characters: " + characterCount);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -