我正在创建一个简单的测验应用,并希望始终显示不同的问题;这是我的代码,关于随机数的部分是在“nextQuestion()”中,但似乎没有用,控制台上没有出现错误
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.Linq;
public class test : MonoBehaviour {
public Text question;
public Text answerA;
public Text answerB;
public Text answerC;
public Text answerD;
public Text answersInfo;
public int themeid;
public string[] questions; //store all questions
public string[] choicesA; //store all choices A
public string[] choicesB; //store all choices B
public string[] choicesC; //store all choices C
public string[] choicesD; //store all choices D
public string[] right; //store all right choices
private int questionid;
private int id;
private bool checkRandom = true;
private float totalRight;
private float totalQuestions;
private float average;
private int finalNote;
List<int> idUsed = new List<int>();
void Start()
{
id = Random.Range(0, 4);
questionid = 0;
totalQuestions = 5;
question.text = questions[id];
answerA.text = choicesA[id];
answerB.text = choicesB[id];
answerC.text = choicesC[id];
answerD.text = choicesD[id];
idUsed.Add(id);
answersInfo.text = "Answering question " + (questionid + 1).ToString() + " out of " + totalQuestions.ToString();
}
public void response(string choice)
{
switch (choice)
{
case "A":
if (choicesA[id] == right[id])
{
totalRight += 1;
}
break;
case "B":
if (choicesB[id] == right[id])
{
totalRight += 1;
}
break;
case "C":
if (choicesC[id] == right[id])
{
totalRight += 1;
}
break;
case "D":
if (choicesD[id] == right[id])
{
totalRight += 1;
}
break;
}
nextQuestion();
}
void nextQuestion()
{
questionid += 1;
if (questionid <= (totalQuestions - 1))
{
totalQuestions = 5;
id = Random.Range(0, 4);
while(checkRandom)
{
if (idUsed.Contains(id))
{
id = Random.Range(0, 4);
}
else
{
idUsed.Add(id);
checkRandom = false;
}
}
question.text = questions[id];
answerA.text = choicesA[id];
answerB.text = choicesB[id];
answerC.text = choicesC[id];
answerD.text = choicesD[id];
answersInfo.text = "Answering question " + (questionid + 1).ToString() + " out of " + totalQuestions.ToString();
}
else
{
average = 10 * (totalRight / totalQuestions);
finalNote = Mathf.RoundToInt(average);
if (finalNote > PlayerPrefs.GetInt("finalNote" + themeid.ToString()))
{
PlayerPrefs.SetInt("finalNote" + themeid.ToString(), finalNote);
PlayerPrefs.SetInt("totalRight" + themeid.ToString(), (int)totalRight);
}
PlayerPrefs.SetInt("finalTempNote" + themeid.ToString(), finalNote);
PlayerPrefs.SetInt("totalRight" + themeid.ToString(), (int)totalRight);
SceneManager.LoadScene("FinalNote");
}
}
答案 0 :(得分:0)
从你的问题不清楚“似乎不起作用”是指什么。如果你能提供一个关于完全发生的更具体的描述,以及它与你想要发生的事情的精确程度,会更好。
那就是说,看着代码,在我看来你的关注可能是你希望程序在测验期间永远不会重复一个问题,但你有时候不止一次得到同样的问题。
如果这是对您的问题的准确描述,那么主要原因是您从未将checkRandom
标志设置回true
。因此,一旦您成功选择了一个问题,代码将永远不会验证后来选择的问题是否已被提出。
解决此问题的一种方法是在选择新问题之前(例如checkRandom
方法)将true
设置为nextQuestion()
。但实际上,你根本不需要旗帜。您可以将Contains()
条件作为循环的实际条件。例如:
if (questionid <= (totalQuestions - 1))
{
totalQuestions = 5;
id = Random.Range(0, 4);
while(idUsed.Contains(id))
{
id = Random.Range(0, 4);
}
idUsed.Add(id);
question.text = questions[id];
answerA.text = choicesA[id];
answerB.text = choicesB[id];
answerC.text = choicesC[id];
answerD.text = choicesD[id];
answersInfo.text = "Answering question " + (questionid + 1).ToString() + " out of " + totalQuestions.ToString();
}
注意:
totalQuestions
字段。if (questionid < totalQuestions)
表达该条件的方式比if (questionid <= (totalQuestions - 1))
更好。totalQuestions
,当您觉得这些值实际上只是整数时,您已声明为float
,即变量应为int
而不是float
。answersInfo.text
属性值的位置)应该封装在您可以为此目的调用的辅助方法中。List<int>
集合的idUsed
就可以了。但是你应该记住HashSet<T>
类(即HashSet<int>
)的集合,你需要快速,有效的遏制测试。该列表需要搜索整个数据结构,而哈希集可以立即确定包含,而无需检查集合中的多个位置。
如果以上内容无法解决您的问题,请提供可靠地重现问题的Minimal, Complete, and Verifiable code example,以及该代码的功能以及您希望它做什么的详细说明。