嘿伙计们我正在制作游戏来帮助我学习决赛。它基本上是闪存卡。我希望程序通过随机选择一个来打印我的问题和答案。事情是我不希望他被挑选两次相同的随机数。我如何确保相同的数字不会被挑选两次直到我出问题?我试图将每个随机数存储在黑名单中,这样我就可以在程序打印之前检查数字是否已经存在。
import java.util.ArrayList;
import java.util.Random;
public class Games {
static ArrayList<Integer> blacklist = new ArrayList<Integer>();
static int number;
static String[][] yes = {{"Apostolic Orgin", "Comes form the apostles"},
{"Biblical inerrancy", "the doctrine that the books are free from error reading the truth"},
{"Divine Inspiration", "the assistance the holy spirit gave the authors or the bible so they could write"},
{"fundamentalist approach", "interpretation of the bible and christian doctrine based on the literal meaning og bible's word"}, {"pentateuch", "first 5 books of old testament"}};
public static void main(String[] args) {
Printer(pickQuestion(), pickAnswer());
}
public static int RandomNumber(int i) {
int c;
Random r = new Random();
c = r.nextInt(i);
return c;
}
public static void Printer(String question, String answer) {
System.out.println("Question: " + question);
System.out.println("Answer: " + answer);
}
public static String pickQuestion() {
number = RandomNumber(yes.length);
return yes[number][0];
}
public static String pickAnswer() {
return yes[number][1];
}
}
答案 0 :(得分:0)
最简单的选择是预先混洗问题列表,要么直接弄乱问题并回答数组,要么创建一个从1到n的整数数组,其中是问题的数量,洗牌,然后加载数组中该值指向的位置中的“卡”。 也就是说,创造 [1,2,3,4,5] 洗牌: [5,1,3,4,2] 然后加载问题5,然后加载问题1等。
后一种选择会使一些事情变得更容易,尤其是调试!
按顺序创建数组是一个简单的for循环,并且还有很多用于混洗的标准算法。 Random shuffling of an array