【发布时间】:2018-09-12 09:00:56
【问题描述】:
所以我有 10 个问题,所以当游戏开始时,例如开始问题是“4 out of 10”,那么如果下一个问题是随机的,则为 “10 out of 10” 游戏结束。我想要的是随机抽取 10 个问题:
private int idMode;
public Text question;
public Text answerA;
public Text answerB;
public Text answerC;
public Text answerD;
public Text infoAnswer;
public Text stat;
public string[] questions;
public string[] alternativeA;
public string[] alternativeB;
public string[] alternativeC;
public string[] alternativeD;
public string[] correct;
private int idQuestion;
private float points;
private float fact;
private float average;
private int results;
void Start () {
idMode = PlayerPrefs.GetInt ("idMode");
idQuestion = 0;
fact = questions.Length;
question.text = questions [idQuestion];
answerA.text = alternativeA [idQuestion];
answerB.text = alternativeB [idQuestion];
answerC.text = alternativeC [idQuestion];
answerD.text = alternativeD [idQuestion];
infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
public void answer(string alternative)
{
if (alternative == "A") {
if (alternativeA [idQuestion] == correct [idQuestion]) {
points += 1;
} else {
}
}
if (alternative == "B") {
if (alternativeB [idQuestion] == correct [idQuestion]) {
points += 1;
} else {
}
}
if (alternative == "C") {
if (alternativeC [idQuestion] == correct [idQuestion]) {
points += 1;
} else {
}
}
if (alternative == "D") {
if (alternativeD [idQuestion] == correct [idQuestion]) {
points += 1;
} else {
}
}
nextQuestion ();
}
void nextQuestion()
{
idQuestion += Random.Range(0,10);
if(idQuestion <= (fact-1))
{
question.text = questions [idQuestion];
answerA.text = alternativeA [idQuestion];
answerB.text = alternativeB [idQuestion];
answerC.text = alternativeC [idQuestion];
answerD.text = alternativeD [idQuestion];
stat.text = " Correct: " + points.ToString () + "";
infoAnswer.text = (idQuestion + 1).ToString() + " of " + fact.ToString () + "";
}
else
{
average = 10 * (points / fact);
results = Mathf.RoundToInt (average);
if (results > PlayerPrefs.GetInt ("results" + idMode.ToString ())) {
PlayerPrefs.SetInt ("results" + idMode.ToString (), results);
PlayerPrefs.SetInt ("points" + idMode.ToString (), (int)points);
}
PlayerPrefs.SetInt ("resultsTemp" + idMode.ToString (), results);
PlayerPrefs.SetInt ("pointsTemp" + idMode.ToString (), (int)points);
Application.LoadLevel("results");
}
}
}
【问题讨论】: