我目前正试图让我的java程序打印出来不打印出重复的法律。该软件是随机挑选打印1至3法律,但我不想重复。我对java还是一个新手,到目前为止只写了一些程序。这是迄今为止我写的最复杂的一个。hashset删除数组Java中的重复项
现在我想要软件再次运行该随机,如果它发现重复,直到打印中没有重复。有没有办法做到这一点?
/**
*Seeds is a random number to pick which if statement will be used.
* telling the software how many laws to use during its random
* generation process
*
*/
Random seeds = new Random();
seed = seeds.nextInt(3) + 1;
if (seed == 1){
/**Beginning of random law selection for universe. Must find way
*Must find way to get rid of duplicate answers
*/
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
System.out.println("Law:" + Laws[index]);
}
else if (seed == 2) {
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
int index2 = random.nextInt(Laws.length);
System.out.println("Law:" + Laws[index] + "," + Laws[index2]);
}
else {
final String[] Laws = {"Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic"};
Random random = new Random();
int index = random.nextInt(Laws.length);
int index2 = random.nextInt(Laws.length);
int index3 = random.nextInt(Laws.length);
System.out.println("Law: " + Laws[index] + "," + Laws[index2] +
"," + Laws[index3]);
}
更少的代码行,更少的问题,同一个String数组的3个声明不好,声明一次,如果其他块 –