目标是从用户那里得到一个句子输入,对其进行标记,然后只给出关于前三个单词的信息(单词本身、长度,然后平均前 3 个单词的长度)。我不确定如何将标记转换为字符串。我只需要一些指导 - 不知道如何进行。到目前为止我有这个:
public static void main(String[] args) {
String delim = " ";
String inSentence = JOptionPane.showInputDialog("Please enter a sentence of three or more words: ");
StringTokenizer tk = new StringTokenizer(inSentence, delim);
int sentenceCount = tk.countTokens();
// Output
String out = "";
out = out + "Total number of words in the sentence: " +sentenceCount +"\n";
JOptionPane.showMessageDialog(null, out);
}
我真的很感激任何指导!