Arrays 我如何让我的程序继续运行?
嘿,伙计们,我正在做一个游戏来帮助我准备期末考试。基本上是闪存卡。我想程序打印出我的问题和答案,随机选择一个。问题是我不希望同一个随机数被选两次。我如何确保在我没有问题之前不会选同一个号码两次?我正在尝试将每个随机数存储在黑名单中,以便在程序打印之前检查该数字是否已经存在Arrays 我如何让我的程序继续运行?,arrays,arraylist,Arrays,Arraylist,嘿,伙计们,我正在做一个游戏来帮助我准备期末考试。基本上是闪存卡。我想程序打印出我的问题和答案,随机选择一个。问题是我不希望同一个随机数被选两次。我如何确保在我没有问题之前不会选同一个号码两次?我正在尝试将每个随机数存储在黑名单中,以便在程序打印之前检查该数字是否已经存在 import java.util.ArrayList; import java.util.Random; public class Games { static ArrayList<
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];
}
}
import java.util.ArrayList;
导入java.util.Random;
公开课游戏{
静态ArrayList黑名单=新建ArrayList();
静态整数;
静态字符串[][]yes={{“使徒起源”,“来自使徒”},
{《圣经》的无误性“,《圣经》无误阅读真理的教义”},
{“神的启示”,“圣灵给予作者或圣经的帮助,使他们能够写作”},
{“原教旨主义方法”,“根据圣经单词的字面意思解释圣经和基督教教义”},{“摩西五经”,“旧约前5卷”};
公共静态void main(字符串[]args){
打印机(pickQuestion(),pickAnswer());
}
公共静态整数随机数(整数i){
INTC;
随机r=新随机();
c=r.nextInt(i);
返回c;
}
公共静态无效打印机(字符串问题、字符串答案){
System.out.println(“问题:+问题);
System.out.println(“回答:+Answer”);
}
公共静态字符串pickQuestion(){
数字=随机数字(是,长度);
返回是[编号][0];
}
公共静态字符串pickAnswer(){
返回yes[编号][1];
}
}
最简单的方法是对问题列表进行预洗牌,或者直接搅乱问答数组,或者创建一个从1到n的整数数组,其中是问题数,洗牌,然后将“卡”加载到数组中该值所指向的位置。
也就是说,创建
[1,2,3,4,5]
洗牌:
[5,1,3,4,2]
并加载问题5,然后加载问题1,以此类推
后一个选项将使一些事情变得更容易,尤其是调试
按顺序创建数组是一个简单的for循环,还有很多标准的洗牌算法。