我设法创建了一个输入字段,可以通过创建另一个文本字段并将其链接到所述问题来检查和验证问题,例如,如果问题的值为“1”,则答案为“1” ”。然后我进行了文本比较,这样如果用户写的=该文本字段,答案就是正确的。但是,我意识到有时候有人可以写别的东西。例如,在“你对老虎有什么看法”的问题中,不仅有一种可能的答案。因此,我为输入字段执行此操作的方式并不完全正常(是吗?)。
我做了很多研究并发现了字典,但是因为它们只有一个关键值无法帮助,然后我发现了列表,这可能呢?
所以我的问题是,是否有可能以及如何创建一个整数值以某种方式与总体问题的值相关联的列表,以便如果随机值为1,则列表值也为1,然后检查如果写的内容与该随机值的任何答案匹配。
如果我刚才所说的没有意义,这是一个例子:
目前的行为:
调查:你喜欢猫吗?
输入字段:是的,我这样做
隐藏文本领域:是的,我做
输入字段=隐藏文本字段,因此正确
理想行为:
调查:你喜欢猫吗?
INPUT FIELD:我喜欢猫
可能的答案:我喜欢猫,是的我喜欢等等。
INPUT FIELD在列表中包含一个与问题匹配的答案,因此也是正确的。
我以为你可以使用.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说我尝试通过主摄像头中的字典脚本和输入字段中的脚本来实现它,但出于某种原因,当我加载它时,控制台上没有任何工作:
LIST
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
}