0

我正在尝试通读字典并获取其中的所有值,然后检查字典位置中的值是否等于(A),当单击按钮时,如果字典中的值以(A)结尾,那么单词“打印正确”,否则打印“不正确”

但是当我单击按钮时发生的情况是打印每个值的正确性和不正确性。所以它确实可以识别正确答案和错误答案,但我只需要它打印出正确或不正确的答案

该问题发生在 OnGUI 函数中。

该代码允许随机键,这是在单击按钮时选择的问题,其值是显示为按钮的答案,然后单击一个按钮。正确答案是以 (A) 结尾的值。bu单击显示有值名称的按钮,您将检查您是否选择了正确的答案,但它有一个小问题。它打印出所选键中每个值的正确性和不正确性

这是整个代码

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

我假设您正在加载表单的可用答案,可能是在带有按钮的标签或按钮的文本中。无论哪种方式,当您加载答案时,将正确按钮的标记属性设置为“A”。如果点击按钮的标签有一个“A”,那就是正确的。

更改为如此简单且易于适应您已有的结构的东西,将比尝试制作具有内在缺陷的作品要好得多

在按钮的事件处理程序中单击这样的东西应该可以工作:

    void ButtonClck(object sender, eventargs e)
    {
        Button clickedbutton = (Button)sender;
        if ((string)clickedbutton.Tag = "A")
        {
            print ("Correct");
        } 
        else
        {
            print ("Incorrect");        
        }

    }
于 2013-10-17T00:12:17.867 回答
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 回答