-3

我怎样才能随机化这组数组而不得到相同的值?我不知道我做得对不对。当它发生变化时,它如何识别价值?

function Start () 
    {
    if(MasterVar.quiz<10)
    {
        var englishLecture = [
        ["Word that tells us about an action","Verb","Noun","Pronoun","a","0"],
        ["Name of person, place or things","Verb","Noun","Cute","b","0"],
        ["Select the noun","Swim","Shout","Joe","c","0"],
        ["There __ a cat","is","was","can","a","0"],
        ["You __ go home now","can","open","was","a","0"],
        ["She ___ her breakfast","ate","play","sleep","a","0"],
        ["Select the verb","John","is","table","b","0"],    
        ["We ___ planning to go to the movies tonight","were","could","can","b","0"],
        ["What ____ that noise?","Were","Was","Can","b","0"],
        ["What ___ your name?","are","can","is","c","0"]
        ];
        index = Random.Range(0,englishLecture.length);
        while(englishLecture[index][5] == "1")
         {
             index = Random.Range(0,englishLecture.length);
         }
        englishLecture[index][5] == "1";

        TextGUI =  GameObject.FindGameObjectWithTag("Question");
        TextGUI.GetComponent(GUIText).text = englishLecture[index][0];
        sel1 = englishLecture[index][1];
        sel2 = englishLecture[index][2];
        sel3 = englishLecture[index][3];
        rightAns = englishLecture[index][4];
    }
    else if(MasterVar.quiz>=10)
    {
            Application.LoadLevel("Score");
    }
}
4

1 回答 1

1

我假设“0”/“1”值是为了存储一个问题之前是否被问过,而 while 循环是为了选择一个新的随机问题,直到找到一个未回答的问题。

您已经将值从“0”更改为“1”,但主要问题是一旦设置为“1”,您就再也不会阅读它了。

如果您希望您的应用程序记住一个问题被标记为已回答(“1”),您需要将该数据写入本地文件,并在应用程序启动时从该文件中读取数组。您需要了解文件 I/O 和序列化才能做到这一点:http: //msdn.microsoft.com/en-us/library/vstudio/ms172873.aspx

如果您只需要在第一个随机问题被问到之后再问一个问题,那么您需要重复在第一个问题被标记为已回答后提出问题的代码。您可以通过使用 while 循环、事件或将提问代码封装到函数中来做到这一点。一切都与结构有关!

例如,你想从这个开始:

index = Random.Range(0,englishLecture.length); 

while(englishLecture[index][5] == "1") { 
    index = Random.Range(0,englishLecture.length); 
} 

englishLecture[index][5] == "1";

TextGUI =  GameObject.FindGameObjectWithTag("Question");
TextGUI.GetComponent(GUIText).text = englishLecture[index][0];
sel1 = englishLecture[index][1];
sel2 = englishLecture[index][2];
sel3 = englishLecture[index][3];
rightAns = englishLecture[index][4];

//Add code to manage user interaction here

...对此:

for (var i = 0; i < 5; i++) {  // This will ask 5 questions
    AskRandomQuestion();
}

function AskRandomQuestion() {

    index = Random.Range(0,englishLecture.length); 

    while(englishLecture[index][5] == "1") { 
        index = Random.Range(0,englishLecture.length); 
    } 

    englishLecture[index][5] == "1";

    TextGUI =  GameObject.FindGameObjectWithTag("Question");
    TextGUI.GetComponent(GUIText).text = englishLecture[index][0];
    sel1 = englishLecture[index][1];
    sel2 = englishLecture[index][2];
    sel3 = englishLecture[index][3];
    rightAns = englishLecture[index][4];

    //Add code to manage user interaction here
}

另外,请注意,当所有问题都标记为已回答时,while 子句将无限循环,永远找不到未回答的问题,并且应用程序将崩溃。

更好的设计是将所有未回答的问题提取到一个新数组中(创建一个仅包含未回答问题的数组)。如果该数组不为空,您可以在其中选择一个随机问题。否则,您知道所有问题都已得到解答。

像这样的东西:

function AskRandomQuestion() {

    unasweredQuestions = new Array();

    for (var i = 0; i < englishLecture.length; i++) {
        if (englishLecture[i][5] == "0") {
            unansweredQuestions.push(englishLecture[i]);
        }
    }

    if (unansweredQuestions.length > 0) {
        selectedQuestion = unansweredQuestions[Random.Range(0,unansweredQuestions.length)]
        selectedQuestion[5] == "1";

        TextGUI =  GameObject.FindGameObjectWithTag("Question");
        TextGUI.GetComponent(GUIText).text = selectedQuestion[0];
        sel1 = selectedQuestion[1];
        sel2 = selectedQuestion[2];
        sel3 = selectedQuestion[3];
        rightAns = selectedQuestion[4];

        // Add code to manage user interaction here
    } else {
        // All questions were answered, quit or reset
    }

}

这些都是未经测试的代码,可以从一些优化中受益,但它应该可以帮助您入门。

于 2013-07-27T09:32:28.527 回答