我参加了计算机科学项目课程,我一直在做一个项目,但我被困住了。我必须编写一个有 5 个谜语和 5 个答案的程序。我在尝试随机重复数组中的元素而不重复相同元素两次时遇到了麻烦。我知道它与 int [] 重复有关,但除此之外我不知道我在做什么。我的老师告诉我以某种方式使用此代码:
int[]repeat= new int[5]; This goes with other array declarations.
repeat = {0,0,0,0,0}
index = rand.nextInt(5);
while(repeat[index] ==1) This goes before you post a riddle
{
index = rand.nextInt(5);
}
System.out.println("The riddle is " + repeat[Index]==1);
我将如何使用它来随机重复数组中的元素,而相同的元素不会重复两次?