我对 Java 编码非常陌生,并且在过去几个小时里一直困扰着我。我正在创建一个非常小而简单的应用程序,它将生成一首诗。我有四个带有“名词”、“动词”、“形容词”和“副词”的 ArrayList。这首诗将从每个列表中取出一个单词并将其组合成一首四行诗,每个列表中每行一个单词,如下所示:
Adverbs Adjectives Nouns Verbs
Adverbs Adjectives Nouns Verbs
Adverbs Adjectives Nouns Verbs
Adverbs Adjectives Nouns Verbs
这将创建一首毫无意义的诗......无论如何......我的问题是结果看起来像这样:
public void generatePoem() {
adjectives.add("adjective1");
adjectives.add("adjective2");
adjective.add("adjective3");
adjective.add("adjective4");
nouns.add("noun1");
nouns.add("noun2");
nouns.add("noun3");
nouns.add("noun4");
verbs.add("verb1");
verbs.add("verb2");
verbs.add("verb3");
verbs.add("verb4");
adverbs.add("adverb1");
adverbs.add("adverb2");
adverbs.add("adverb3");
adverbs.add("adverb4");
String thepoem = "";
for (int i=0;i<16;i++) {
int count = i + 1;
int random = (int)(Math.random()*4);
if (count % 2 == 0) {
thepoem += adjectives.get(random).toString() + " ";
}
else if (count % 3 == 0) {
thepoem += nouns.get(random).toString() + " ";
}
else if (count % 4 == 0) {
thepoem += verbs.get(random).toString() + "\n";
}
else {
thepoem += adverbs.get(random).toString() + " ";
}
}
JOptionPane.showMessageDialog(null, thepoem);
}
}
结果是随机的,但总是这样:
Adverb Adjective Noun Adjective(?!?!)
Adverb Adjective Noun Adjective(?!?!)
Adverb Adjective Noun Adjective(?!?!)
Adverb Adjective Noun Adjective(?!?!)
动词在哪里?
我似乎无法找到解决方案...有人请帮助我吗?同样,我仍处于 Java 编程的早期阶段。