我有一个任务是创建一个生成随机“诗歌”的程序。这是我的代码:
package poem;
import java.util.Random;
public class Poem {
public static void main(String[] args) {
String rhyme1[] = {"ball", "call", "mall", "hall", "guy named Paul"};
String rhyme2[] = {"cat","hat", "bat", "rat", "mat", "guy named Pat"};
String nouns[] = {"you", "I", "girl", "boy", "man", "woman"};
String verbs[] = {"run", "jump", "dive", "sink", "fall", "collapse", "swim", "love"};
String others[] = {"like a", "into a", "nothing like a"};
Random gen = new Random();
String currentNoun = nouns[gen.nextInt(nouns.length)];
String currentVerb = verbs[gen.nextInt(verbs.length)];
String currentRhyme1 = rhyme1[gen.nextInt(rhyme1.length)];
String currentRhyme2 = rhyme2[gen.nextInt(rhyme2.length)];
String currentOther = others[gen.nextInt(others.length)];
System.out.printf("%s %s %s %s \n", currentNoun, currentVerb, currentOther, currentRhyme1);
System.out.printf("%s %s %s %s \n", currentNoun, currentVerb, currentOther, currentRhyme1);}}
我的问题是,当我运行它时,我两次都得到完全相同的行。如何每次获得不同的随机生成的行?提前致谢。