Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
生成与以前使用的不同的随机数c#_C#_Android_Unityscript - Fatal编程技术网

生成与以前使用的不同的随机数c#

生成与以前使用的不同的随机数c#,c#,android,unityscript,C#,Android,Unityscript,我正在创建一个简单的测验应用程序,并希望始终显示不同的问题;这是我的代码,关于随机数的部分在“nextQuestion()”中,但似乎不起作用,控制台上没有出现错误 using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections.Generic; using System.Linq; public class test : MonoBehaviour {

我正在创建一个简单的测验应用程序,并希望始终显示不同的问题;这是我的代码,关于随机数的部分在“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");
    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.SceneManagement;
使用System.Collections.Generic;
使用System.Linq;
公共课堂测试:单一行为{
公共文本问题;
公共文本回答;
公共文本应答;
公共文本应答;
公共文本应答;
公共文本应答信息;
公共国际节;
公共字符串[]问题;//存储所有问题
公共字符串[]choicesA;//存储所有选项A
公共字符串[]choicesB;//存储所有选项B
公共字符串[]choicesC;//存储所有选项C
公共字符串[]choicesD;//存储所有选项D
公共字符串[]right;//存储所有正确的选择
私人智力测验;
私有int-id;
private bool checkRandom=true;
私权;
私人问题;
私人浮动平均线;
私人财务通知;
List idUsed=新列表();
void Start()
{
id=随机范围(0,4);
问题ID=0;
问题总数=5;
question.text=问题[id];
answerA.text=choicesA[id];
answerB.text=choicesB[id];
answerC.text=choicesC[id];
answerD.text=choicesD[id];
添加(id);
answersInfo.text=“Answering question”+(问题ID+1).ToString()+“+totalQuestions.ToString()”;
}
公共无效响应(字符串选择)
{
开关(选择)
{
案例“A”:
if(choicesA[id]==右[id])
{
totalRight+=1;
}
打破
案例“B”:
if(choicesB[id]==右[id])
{
totalRight+=1;
}
打破
案例“C”:
if(choicesC[id]==右[id])
{
totalRight+=1;
}
打破
案例“D”:
if(choicesD[id]==右[id])
{
totalRight+=1;
}
打破
}
nextQuestion();
}   
void nextQuestion()
{
问题ID+=1;
if(questionid 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);
场景管理器。加载场景(“最终注释”);
}
}

你的问题不清楚“似乎不起作用”是什么意思。如果你能提供一个更具体的描述,说明到底发生了什么,以及这与你想要发生的有多大的不同,那就更好了

也就是说,在我看来,看代码,您可能担心的是,您希望程序在测试期间不会重复一个问题,但有时您会多次收到相同的问题

如果这是对问题的准确描述,那么主要原因是您从未将
checkRandom
标志设置回
true
。因此,一旦您成功地选择了一个问题,代码将永远不会验证随后选择的问题是否尚未被询问

解决此问题的一种方法是在选择新问题之前(即在
nextQuestion()
方法中)将
checkRandom
设置为
true
。但实际上,你根本不需要国旗。您可以将
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();

}

if(问题ID)您说“但似乎不起作用”。您在visual studio中调试过它吗?什么行为“似乎不起作用”?@PhillipH我有,但我用它来让测验中的问题不会重复,每当我在unity中测试应用程序时都会发生这种情况。非常感谢!正如你可能知道的那样,我对这一点很陌生,并且倾向于编写看起来很符合逻辑的代码,因为我不能依赖经验-很抱歉我没有经验。代码可以现在,在每个循环中生成不同的数字,但当“nextQuestion()时”运行最后一个问题,unity冻结。我怀疑这是因为while循环启动了一个无休止的循环,但我看不出这是怎么可能的,也看不到如果没有良好的解决方案,我无法确定您的问题可能是什么。但您的猜测似乎很有可能。您将
totalQuestions
设置为
5
,但您已被选中仅从四种可能性(0、1、2和3)中提取问题ID值。因此,当您到达值为
4
questionid
变量时,您已经选择了四个不同的值。您需要允许更大范围的随机值,或者选择更少的问题。