C# 检查字典的多个值以验证答案
我设法创建了一个输入字段,通过创建另一个文本字段并将其链接到所述问题,可以对问题进行检查和验证,因此,例如,如果问题的值为“1”,则答案为“1”。然后我做了一个文本比较,这样如果用户写的内容=该文本字段,那么答案将是正确的。 然而,我意识到有时有人可以写其他东西。例如,在“你认为老虎怎么样”的问题中,并不是只有一个可能的答案。因此,我对输入字段的处理方式并不完全有效(是吗?) 我做了大量的研究,发现了字典,但因为它们只有一个键值,这对我没有帮助,然后我发现了列表,这可能是什么 所以我的问题是,是否有可能,以及如何创建一个整数值列表,以某种方式与首要问题的值相关联,这样,如果随机值为1,那么列表值也为1,然后检查所写内容是否与该随机值匹配 如果我刚才说的没有道理,下面是一个例子: 当前行为: 调查:你喜欢猫吗 输入字段:是的,我知道 隐藏文本字段:是的,我知道 输入字段=隐藏的文本字段,因此正确 理想行为: 调查:你喜欢猫吗 输入框:我确实喜欢猫 可能的答案:我确实喜欢猫,是的,我喜欢等等 输入字段在列表中包含与问题匹配的答案,因此是正确的 我以为您可以使用C# 检查字典的多个值以验证答案,c#,unity3d,C#,Unity3d,我设法创建了一个输入字段,通过创建另一个文本字段并将其链接到所述问题,可以对问题进行检查和验证,因此,例如,如果问题的值为“1”,则答案为“1”。然后我做了一个文本比较,这样如果用户写的内容=该文本字段,那么答案将是正确的。 然而,我意识到有时有人可以写其他东西。例如,在“你认为老虎怎么样”的问题中,并不是只有一个可能的答案。因此,我对输入字段的处理方式并不完全有效(是吗?) 我做了大量的研究,发现了字典,但因为它们只有一个键值,这对我没有帮助,然后我发现了列表,这可能是什么 所以我的问题是,是
.Contains
函数,但我不知道如何将其链接在一起
编辑:
我试图通过创建一个字典和搜索一个键来解决这个问题(我认为这是正确的方法),但出于某种原因,这段代码在检查时甚至不起作用?(好像.containsKey函数不起作用?)
publicstringquestions=“hi;wird;by”;
字典测试器=新字典();
//用于初始化
无效开始()
{
增加(1,问题);
添加(2,“你好”);
添加第(3)条,“由”);
添加(4,“hi”);
添加(5,“再见”);
}
//每帧调用一次更新
无效更新()
{
}
公共无效hello()
{
if(测试仪容器(2))
{
字符串值=测试仪[2];
Debug.Log(“正确”);
}
}
编辑1:
按照trashr0X所说的,我尝试在主摄像头中使用字典脚本,在inputfield中使用脚本,但由于某种原因,当我加载它时,控制台上没有任何工作:
列表
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!");
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用System.Linq;
公共课堂实践:单一行为
{
字典测试器=新字典();
List possibleAnswersToQuestionZero=新列表();
//用于初始化
无效开始(){
可能回答问题0。添加(“你好”);
可能回答问题0。添加(“通过”);
添加(0,可能的回答为0);
}
//每帧调用一次更新
无效更新()
{
}
公共无效hello()
{
var-toCheck=tester[0].FirstOrDefault(x=>x==GameController.hello);
if(toCheck!=null)
{
Log(“嗨!”);
}
}
}
输入字段
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;
}
}
公共类QAClass07
{
公共字符串答案=”;
公共字符串问题=”;
QAClass07结果=新QAClass07();
}
公共静态字符串hello;
void Start()
{
游戏对象a=游戏对象;
hello=a.transform.Find(“Text”).GetComponent().Text;
}
//注册用户写入的内容
公共void getInput(字符串猜测)
{
//假设有人进入了什么东西
如果(GetComponent()!=null)
{
hello=getComponentChildren().text;
}
}
只需使用字典
,然后将所有答案添加到相应的问题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
}
}
var-questions=新列表{“hi”,“weird”,“by”};
var tester=新字典();
//用于初始化
无效开始()
{
增加(1,问题);
Add(2,新列表{“hello”});
添加(3,新列表{“by”});
添加(4,新列表{“hi”});
添加(5,新列表{“bye”});
}
公共无效hello()
{
if(测试仪容器(2))
{
var answers=测试仪[2]??新列表();
//现在,所有答案都链接到answers变量中id为2的问题
}
}
只需使用字典
,然后将所有答案添加到相应的问题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
}
}
var-questions=新列表{“hi”,“weird”,“by”};
var tester=新字典();
//用于初始化
无效开始()
{
增加(1,问题);
Add(2,新列表{“hello”});
添加(3,新列表{“by”});
添加(4,新列表{“hi”});
添加(5,新列表{“bye”});
}
公共无效hello()
{
if(测试仪容器(2))
{
var answers=测试仪[2]??新列表();
//现在,所有答案都链接到answers变量中id为2的问题
}
}
“我做了很多研究,发现了字典,但因为它们只有一个键值,这没有帮助,然后我发现了列表,这可能会有帮助吗?”
是的,a确实由特定类型的键值对组成;您可以将其键的类型声明为int
(对应于当前问题的索引),并将其值的类型声明为List
,以保存该问题的可能答案
// 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);
“我做了很多研究,发现了字典,b
// 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
}