我正在尝试通读字典并获取其中的所有值,然后检查字典位置中的值是否等于(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");
}
}
}
}
}
}