0

ディクショナリを読み取ってその中のすべての値を取得しようとしています。ディクショナリの値が (A) で終わる場合、ボタンをクリックしたときにディクショナリの場所の値が (A) と等しいかどうかを確認してから、"正しい」が印刷され、そうでない場合は「正しくない」が印刷されます

しかし、ボタンをクリックすると、各値の正誤が出力されます。正しい答えと間違った答えを識別しますが、正しいか間違っているかを印刷するだけで済みます

この問題は OnGUI 関数で発生します。

このコードは、ボタンのクリック時に選択される質問であるランダム キーと、ボタンとして表示される回答であるその値を許可し、次にボタンをクリックします。正解は (A) で終わる値です。値の名前が表示されているボタンをクリックすると、正しい答えを選択したかどうかを確認できますが、少し問題があります。選択したキーの各値の正誤を出力します

ここにコード全体があります

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class testQuestions : MonoBehaviour {

  Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>();
    string []vl ;
    string ky;
    int Qnum;
    string A;
    int indx;
    // Use this for initialization
    void Start () {
        dictionary.Add("ups", new string[] {"updegree", "popup (A)"});
        dictionary.Add("down aroud the place like this ", new string[] {"sun", "bun(A)","art"});
        dictionary.Add("left", new string[] {"higi (A)", "migi"});
        dictionary.Add("right", new string[] {"een", "yyo(A)"});
    }

    // Update is called once per frame
    void Update () {



        if (Input.GetKeyDown("q"))
        {
            GenerateRandoms();
        }


    }

    void GenerateRandoms()
    {


        string[] keys = new string[dictionary.Count];//get the dictionary count and store in array
dictionary.Keys.CopyTo(keys, 0);

var index = Random.Range(0, keys.Length);
var key = keys[index];
var value = dictionary[key];
        ky= key;
        vl = value;

foreach (string ku in value)
{

            // create buttons with the ku text as the button text 


            indx = index;
            Qnum +=1;   

            A = ku;


        }
        //---------- remove each after answer button click
        //dictionary.Remove(key); // remove key after it is already displayed. this must only be exicuted at the end of the function

    }

    void OnGUI ()
    {
        int charlength = 0;
        foreach(char NumOfChar in ky)
        {
            charlength ++;
        }
        GUI.TextArea(new Rect (0,0,charlength*10 + 100,20),"Key is " + ky /*+ " value is " + string.Join(",", vl) + " position is " +int.Parse(indx.ToString())*/);

        for (int i = 0; i < dictionary[dictionary.Keys.ElementAt(indx)].Length; i++)
        {

        if (GUI.Button (new Rect(0, 30 + Screen.height-Screen.height + 40* i, 100, 20),dictionary[dictionary.Keys.ElementAt(indx)][i]))
        {


                for (int i2 = 0; i2 < dictionary[dictionary.Keys.ElementAt(indx)].Length; i2++)
                {
                    string Answer = dictionary[dictionary.Keys.ElementAt(indx)][i2];

            if (Answer.EndsWith("(A)"))
            {
                print ("Correct");
            } 
            else
            {
                print ("Incorrect");        
            }
                }



        }
        }


    }
}
4

2 に答える 2

0

これを実現する方法は、最初のボタン クリック イベントの後に if ステートメントを使用するだけです。そのボタンがそのテキストを取得するソースが正しい答えであるかどうかを確認します。

if (GUI.Button (new Rect(0, 30 + Screen.height-Screen.height + 40* i, 100, 20),dictionary[dictionary.Keys.ElementAt(indx)][i]))
        {
if (dictionary[dictionary.Keys.ElementAt(indx)][i].EndsWith ("(A)"))
                {
                    print ("Correct");
            } 
            else
            {
                print ("Incorrect");        


                }
    }
于 2013-10-17T01:41:10.787 に答える