2016-07-16 18 views
-2

我正在創建一個簡單的測驗應用程序,並且希望始終顯示不同的問題;這裏是我的代碼,關於隨機數的部分是「nextQuestion()」,但似乎沒有工作,沒有錯誤出現在控制檯上生成與以前使用的編號不同的隨機數c#

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"); 
    } 
} 
+2

你說「但似乎沒有工作」。你在Visual Studio中調試過它嗎?它是什麼行爲「似乎並不奏效」? – PhillipH

+0

@PhillipH我有,但我用它在測驗中提出問題不會重複自己,每當我在統一測試應用程序時會發生這種情況。 –

回答

0

它不是從你的問題不清楚是什麼「似乎並不上班「的意思。如果您提供關於究竟發生了什麼的更具體描述,以及這與您希望發生的情況有何不同,那將會更好。

這就是說,看代碼,在我看來你的關心可能是你期待的節目從來沒有在考試中重複的問題,而是你有時也得到了同樣的問題一次以上。

如果這是對您問題的準確描述,那麼主要原因是您從未將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(); 

} 

注:

  • 我看不出有任何理由設置totalQuestions場在上面的代碼。
  • 恕我直言if (questionid < totalQuestions)是比if (questionid <= (totalQuestions - 1))更好地表達這種情況。
  • 您有很多字段,包括totalQuestions,您聲明爲float,在我看來這些值實際上只是整數,即變量應該是int而不是float
  • 你應該養成不復制/粘貼代碼的習慣。你的代碼元素,例如你設置answersInfo.text屬性值的地方,應該封裝在一個你可以爲此調用的幫助器方法中。
  • 對於ID值的相對較短的列表,idUsed集合的List<int>很好。但是,您應該記住HashSet<T>類(即HashSet<int>),您需要快速,高效地進行遏制測試。該列表需要搜索整個數據結構,而散列集可以立即確定包含,而不必檢查集合中的多個位置。
  • 最後,雖然您用於選擇隨機問題ID值的循環應該可以正常工作,但這樣做的做法相當笨拙和低效。更好的方法是問題ID值shuffle an array,然後按順序從混洗陣列中選擇ID。


如果上述方法不解決您的問題,請提供一個良好的Minimal, Complete, and Verifiable code example可靠地再現問題,用的代碼做什麼的詳細介紹,你想讓它做什麼,而不是沿。

+0

非常感謝!正如你可能知道的那樣,我對此很陌生,並且傾向於寫一些看起來符合邏輯的代碼,因爲我不能依賴經驗 - 所以對於我的不敬之心感到抱歉。現在,代碼可以在每個循環中生成不同的數字,但是,當「nextQuestion()」運行於最後一個問題時,統一就會凍結。我懷疑這是因爲while循環開始了一個無限循環,但我看不出這是怎麼可能的,我可以修復它 –

+0

沒有一個好的[mcve],我不可能確定你的問題可能是什麼。但你的猜測似乎很可能。您將'totalQuestions'設置爲'5',但您只從四種可能性(0,1,2和3)中選擇問題ID值。因此,當你到達'questionid'變量的值爲'4'時,你已經選擇了四個不同的值。你或者需要允許更大範圍的隨機#,或者選擇更少的問題。 –