检查字典的多个值以验证答案



我设法创建了一个输入字段,可以通过创建另一个文本字段并将其链接到该问题来检查和验证问题,因此例如,如果问题的值为"1",则答案将是"1"。然后我做了一个文本比较,这样如果用户写的内容=该文本字段,答案是正确的。 但是,我意识到有时有人可以写别的东西。例如,在"你如何看待老虎"的问题中,不只有一个可能的答案。因此,我对输入字段执行此操作的方式并不完全有效(是吗?

我做了很多研究并发现了字典,但由于它们只有一个键值,所以没有帮助,然后我发现了列表,哪个可能?

所以我的问题是,是否有可能以及如何创建一个整数值的列表以某种方式链接到总体问题的值,因此如果随机值为 1,则列表值也是 1,然后检查所写的内容是否与该随机值的任何答案匹配。

如果我刚才说的没有意义,这里有一个例子:

当前行为:

调查:你喜欢猫吗?

输入字段:是的,我愿意

隐藏文本字段:是的,我愿意

输入字段 = 隐藏文本字段,因此正确

理想行为:

调查:你喜欢猫吗?

输入字段:我喜欢猫

可能的答案:我喜欢猫,是的,我喜欢等等。

输入字段在列表中包含一个与问题匹配并因此正确的答案。

我以为您可以使用.Contains功能,但我不知道如何将它们链接在一起。

编辑:

我试图通过创建字典和搜索键来解决这个问题(我相信这是正确的方法(,但由于某种原因,这段代码在检查它时甚至不起作用?(就像 .containsKey 函数不起作用一样?

public string questions = "hi;weird;by";
Dictionary<int, string> tester = new Dictionary<int, string>();
// Use this for initialization
void Start () 
{     
tester.Add(1, questions);
tester.Add(2, "hello");
tester.Add(3, "by");
tester.Add(4, "hi");
tester.Add(5, "bye");
}
// Update is called once per frame
void Update () 
{       
}
public void hello ()
{
if(tester.ContainsKey(2))
{
string value = tester[2];
Debug.Log("Correct");
}
}

编辑 1:

按照 trashr0X 所说的,我尝试通过在主摄像头中使用字典脚本并在输入字段中使用脚本来执行此操作,但由于某种原因,当我加载它时,控制台上没有任何工作:

列表

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class Listpractice : MonoBehaviour 
{
Dictionary<int, List<string>> tester = new Dictionary<int, List<string>>();
List<string> possibleAnswersToQuestionZero = new List<string>(); 
// Use this for initialization
void Start () {
possibleAnswersToQuestionZero.Add("Hello");
possibleAnswersToQuestionZero.Add("By");
tester.Add(0, possibleAnswersToQuestionZero);     
}
// Update is called once per frame
void Update ()
{       
}
public void hello ()
{
var toCheck = tester[0].FirstOrDefault(x => x == GameController.hello);
if (toCheck != null)
{
Debug.Log("Hi!");
}
} 
}

输入字段

public class QAClass07
{
public string Answer = "";
public string Question = "";
QAClass07 result = new QAClass07();        
}
public static string hello;   
void Start()
{
GameObject a = gameObject;   
hello = a.transform.Find("Text").GetComponent<Text>().text;
}
// registers what the user writes 
public void getInput(string guess)
{        
// Does something assuming someone enters something
if (GetComponent<InputField>() != null)
{
hello = GetComponentInChildren<Text>().text;
}              
}

只需使用Dictionary<int, List<string>>,然后将所有答案添加到相应的问题 ID 中。

var questions = new List<string> { "hi", "weird", "by" };
var tester = new Dictionary<int, List<string>>();
// Use this for initialization
void Start () 
{     
tester.Add(1, questions);
tester.Add(2, new List<string> { "hello" });
tester.Add(3, new List<string> { "by" });
tester.Add(4, new List<string> { "hi" });
tester.Add(5, new List<string> { "bye" });
}

public void hello ()
{
if(tester.ContainsKey(2))
{
var answers = tester[2] ?? new List<string>();
// now you have all answers linked to question with id 2 in answers variable
}
}

"我做了很多研究,发现了字典,但由于它们只有一个键值,所以没有帮助,然后我发现了列表,哪个可能?">

是的,Dictionary<TKey, TValue>确实由特定类型的键值对组成;您可以声明要int的键的类型(对应于当前提出的问题的索引(,并声明要List<string>的值的类型,以保存该问题的可能答案。

// key is question index, value is a list of possible answers for that question
var dictionary = new Dictionary<int, List<string>>();
// list of possible answers for question 0 (random question number chosen for the example)
var possibleAnswersToQuestionZero = new List<string>(); 
possibleAnswersToQuestionZero.Add("Possible Answer to question 0");
possibleAnswersToQuestionZero.Add("Another possible answer to question 0");
// add that list to the dictionary at key 0.
// you should be also checking if the key exists before trying to access it's value, 
// and what happens if the list returned for that key is null or empty.
dictionary.Add(0, possibleAnswersToQuestionZero);

要检查用户提交的答案(假设它保存在名为userInput的变量中(的问题0是否在该键的可能答案列表中,我们将执行以下操作:

// check if the list at dictionary[0] has at least one instance of userInput,
// otherwise return null
var toCheck = dictionary[0].FirstOrDefault(x => x == userInput);
// if the result is not null, the answer was found 
if (toCheck != null)
{
// answer found
}