用于从Java数组中删除重复项的哈希集
我目前正试图让我的java程序在打印时不打印出重复的定律。该软件是随机挑选打印出1至3条法律,但我不想重复。我对java还是一个新手,到目前为止只编写了几个程序。这是迄今为止我写过的最复杂的一篇 现在,我希望软件再次运行随机,如果它发现一个重复,直到没有重复的打印。有什么办法可以这样做吗用于从Java数组中删除重复项的哈希集,java,duplicates,Java,Duplicates,我目前正试图让我的java程序在打印时不打印出重复的定律。该软件是随机挑选打印出1至3条法律,但我不想重复。我对java还是一个新手,到目前为止只编写了几个程序。这是迄今为止我写过的最复杂的一篇 现在,我希望软件再次运行随机,如果它发现一个重复,直到没有重复的打印。有什么办法可以这样做吗 /** *Seeds is a random number to pick which if statement will be used. * telling the software ho
/**
*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]);
}
另一种思考这个问题的方式是;您希望以随机顺序选择值。这样,除非元素多次出现,否则不会得到重复的元素
List<String> laws = Arrays.asList("Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic");
Collections.shuffle(laws);
// select as many as you need
List<String> lawsSelected = laws.subList(0, 3);
List laws=array.asList(“标准物理”、“魔法”、“疯狂科学”,
“灵能”、“次等物理”、“奇异”);
收藏。洗牌(法律);
//根据需要选择任意数量
List lawsSelected=laws.subList(0,3);
思考这个问题的另一种方式是;您希望以随机顺序选择值。这样,除非元素多次出现,否则不会得到重复的元素
List<String> laws = Arrays.asList("Standard Physics", "Magic", "Mad Science",
"Psionics", "Substandard Physics","Exotic");
Collections.shuffle(laws);
// select as many as you need
List<String> lawsSelected = laws.subList(0, 3);
List laws=array.asList(“标准物理”、“魔法”、“疯狂科学”,
“灵能”、“次等物理”、“奇异”);
收藏。洗牌(法律);
//根据需要选择任意数量
List lawsSelected=laws.subList(0,3);
这里有一种方法。你有3个if条件,所以取3个布尔变量,将一个与每个if关联,并在执行if时使其为真
Random seeds = new Random();
seed = seeds.nextInt(3) + 1;
boolean selected[] = new boolean[3];
selected[0] = selected[1] = selected[2] = false;//initially none of the if is executed
if (seed == 1 && !selected[seed-1]){//checking if not previously selected and "seed-1" since array indexes are from 0 to n-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]);
selected[seed-1] = true;
}
else if (seed == 2 && !selected[seed-1]) {
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]);
selected[seed-1] = true;
}
else if (!selected[seed-1]){
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]);
selected[seed-1] = true;
}
这里有一种方法。你有3个if条件,所以取3个布尔变量,将一个与每个if关联,并在执行if时使其为真
Random seeds = new Random();
seed = seeds.nextInt(3) + 1;
boolean selected[] = new boolean[3];
selected[0] = selected[1] = selected[2] = false;//initially none of the if is executed
if (seed == 1 && !selected[seed-1]){//checking if not previously selected and "seed-1" since array indexes are from 0 to n-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]);
selected[seed-1] = true;
}
else if (seed == 2 && !selected[seed-1]) {
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]);
selected[seed-1] = true;
}
else if (!selected[seed-1]){
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]);
selected[seed-1] = true;
}
更少的代码行,更少的问题,相同字符串数组的3个声明不好,首先声明一次if else无块代码行,更少的问题,相同字符串数组的3个声明不好,首先声明一次if else块