2016-07-16 7 views
-2

私は単純なクイズアプリを作成しており、常に異なる質問を表示したいと思います。それはあなたの質問から明らかではない以前に使用されていたものとは異なる乱数を生成する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

あなたは「しかし、うまくいかない」と言います。ビジュアルスタジオでデバッグしましたか?どのような行動は、 "それは動作していないようです"ですか? – PhillipH

+0

@PhillipH私は持っていますが、私はクイズで質問をするためにそれを使用しています。 –

答えて

0

何「は思えないここに私のコードは、乱数についての部分は「nextQuestion()」であるが、動作するようには思えdoesntの、エラーがコンソールに現れませんでした"働く"という意味です。 が正確にであるかどうか、そしてそれがどのように起こりたいのかとはどのくらい正確かをより具体的に説明すると良いでしょう。あなたの懸念プログラムがクイズ中に質問を繰り返さないようにするために、あなたが期待するかもしれませんが、あなた時々が何度も同じ質問多くを得る行うように私にはそれはそうコードを見て、言った

問題の正確な説明がある場合、主な原因は、フラグをtrueに戻すことは決してありません。だから、質問をうまく選択すると、その後選択された質問がまだ尋ねられていないことを確認することはありません。

この問題を解決する方法の1つは、新しい質問を選択する直前に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フィールドを設定する理由は表示されません。
  • IMHO if (questionid < totalQuestions)は、if (questionid <= (totalQuestions - 1))よりその状態を表現する良い方法です。
  • floatのように数値が実際には整数であると思われる場合はtotalQuestionsなどのフィールドがあります。つまり、変数はfloatの代わりにintである必要があります。
  • コードをコピー/貼り付けしないという習慣が必要です。 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'に設定しますが、4つの可能性(0,1,2,3)から質問ID値を選択しています。したがって、 'questionid'変数の値が' 4 'になるときには、すでに4つの異なる値を選択しています。より大きな範囲の乱数を許可するか、より少ない質問を選択する必要があります。 –

関連する問題