我有一个关于在 Java 中制作字符串数组的问题。我想创建一个字符串数组,它将在字符串数组的每个隔间中存储一个特定的单词。例如,如果我的程序扫描What is your deal?
了我想要单词What
andyour
出现在数组中,以便稍后显示它。
我该如何编码?另外,我如何显示它System.out.println();
?
好吧,到目前为止,这是我的代码:
import java.util.Scanner;
import java.util.StringTokenizer;
public class OddSentence {
public static void main(String[] args) {
String sentence, word, oddWord;
StringTokenizer st;
Scanner scan = new Scanner (System.in);
System.out.println("Enter sentence: ");
sentence = scan.nextLine();
sentence = sentence.substring(0, sentence.length()-1);
st = new StringTokenizer(sentence);
word = st.nextToken();
while(st.hasMoreTokens()) {
word = st.nextToken();
if(word.length() % 2 != 0)
}
System.out.println();
}
}
我希望我的程序计算一个句子中的每个单词。如果单词有奇数个字母,就会显示出来。