我正在做一个问答游戏。 我需要对数组进行洗牌,以使问题,选项和答案数组不按顺序出现。
这是三个阵列。
public string[] EasyQuestions =
{
"What is the capital of Australia?",
"Who was Steve Jobs?",
"What number Apollo took Neil Armstrong to the moon?",
"Which metal is heavier, silver or gold?",
"What country has the largest population?"
};
public string[] EasyOptions =
{
"Brisbane |Canberra |Adelaide |Australian Capital Territory",
"CEO of Microsoft |Co-founder of Apple |Creator of IBM |Australian Politician",
"10|11|12|1",
"Silver|Gold|Gold|Silver",
"'Murica|China|India|Australia"
};
public string[] EasyAnswers =
{
"Canberra",
"Apple",
"11",
"Gold",
"China"
};
我希望所有数组都以相同的方式进行洗牌,这样我就不会得到不同选项和不同问题的错误答案?
答案 0 :(得分:4)
正如我在评论中所建议的,如果你将所有内容组合在一起,那么你不必担心保持同步:
public class Question {
private List<string> answers;
public Question(string text, IEnumerable<string> answers, int answer) {
this.Text = text;
this.Answer = answer;
this.answers = new List<string>(answers);
}
public string Text {
get;
private set;
}
public int Answer {
get;
private set;
}
public IEnumerable<string> Answers {
get {
return this.answers;
}
}
public string GetAnswer() {
return this.answers[this.Answer];
}
}
答案 1 :(得分:3)
使用Fisher-Yates shuffle。它在一次通过中将元素混洗。
Random rnd = new Random();
for (int i = 0; i < EasyQuestions.Length - 1; i++) {
int j = rnd.Next(i, EasyQuestions.Length);
string temp = EasyQuestions[j];
EasyQuestions[j] = EasyQuestions[i];
EasyQuestions[i] = temp;
temp = EasyOptions[j];
EasyOptions[j] = EasyOptions[i];
EasyOptions[i] = temp;
temp = EasyAnswers[j];
EasyAnswers[j] = EasyAnswers[i];
EasyAnswers[i] = temp;
}
答案 2 :(得分:0)
不要随意改组阵列。相反,随机播放一个包含0到4的整数列表。由于这似乎是作业,我会让你弄清楚如何使用那个混洗列表。