比较文本数组和输入数组
Compare the text array and the input array
我有一个文本数组和一个字段数组,我需要实现,以便当用户在字段中输入值时,将该值与该字段的文本进行比较。即,例如,数字“12”写在一个文本字段中,用户输入“13”并且“不正确”输出到日志,并且有 6 个这样的字段和文本。 (这是数字 6+6=12 的解法)请告诉我如何做的更好,我开始这样做:
public InputField[] Inputfield1;
public Text[] text1;
public void CurBtn()
{
foreach (InputField inputField in Inputfield1)
{
for (int i = 0; i < text1.Length; i++)
{
for (int j = 0; j < Input_Numer_1.Length; j++)
{
if (text1[i].ToString() == Inputfield1[j].text)
{
Debug.LogFormat($"OK");
}
else{
Debug.LogFormat($"Not correct");
}
}
}
}
Text
是一个 UnityEngine.Object
and Object.ToString
basically returns the GameObject
s name like Text.name
.
您更希望它是 UI.Text.text
if(text1[i].text == Inputfield1[j].text)
但是,为什么不实际存储 int
值并使用 int.TryParse
将您的用户输入转换为数值,而是比较基于 int
的方法更有效。
你还有一个循环到 much 那里..目前你重复输入两次。
对我来说,听起来您甚至不想将每个输入与每个文本进行比较,而是想要比较某些对,例如
[Serializable]
public class CheckPair
{
public InputField Input;
public Text Text;
public bool Compare()
{
var correct = Text.text == Input.text;
Debug.Log($"{correct ? "" : "NOT "} correct", Input);
return correct;
}
}
而在 Inspector 中,宁可将它们单独分配
public CheckPair pairs;
然后
public void CurBtn()
{
foreach (var pair in pairs)
{
pair.Compare();
}
}
或者如果您想知道是否都正确
public void CurBtn()
{
foreach (var pair in pairs)
{
if(!pair.Compare())
{
Debug.Log($"{pair.Input} is not correct!");
return;
}
}
Debug.Log("All are correct!");
}
我有一个文本数组和一个字段数组,我需要实现,以便当用户在字段中输入值时,将该值与该字段的文本进行比较。即,例如,数字“12”写在一个文本字段中,用户输入“13”并且“不正确”输出到日志,并且有 6 个这样的字段和文本。 (这是数字 6+6=12 的解法)请告诉我如何做的更好,我开始这样做:
public InputField[] Inputfield1;
public Text[] text1;
public void CurBtn()
{
foreach (InputField inputField in Inputfield1)
{
for (int i = 0; i < text1.Length; i++)
{
for (int j = 0; j < Input_Numer_1.Length; j++)
{
if (text1[i].ToString() == Inputfield1[j].text)
{
Debug.LogFormat($"OK");
}
else{
Debug.LogFormat($"Not correct");
}
}
}
}
Text
是一个 UnityEngine.Object
and Object.ToString
basically returns the GameObject
s name like Text.name
.
您更希望它是 UI.Text.text
if(text1[i].text == Inputfield1[j].text)
但是,为什么不实际存储 int
值并使用 int.TryParse
将您的用户输入转换为数值,而是比较基于 int
的方法更有效。
你还有一个循环到 much 那里..目前你重复输入两次。
对我来说,听起来您甚至不想将每个输入与每个文本进行比较,而是想要比较某些对,例如
[Serializable]
public class CheckPair
{
public InputField Input;
public Text Text;
public bool Compare()
{
var correct = Text.text == Input.text;
Debug.Log($"{correct ? "" : "NOT "} correct", Input);
return correct;
}
}
而在 Inspector 中,宁可将它们单独分配
public CheckPair pairs;
然后
public void CurBtn()
{
foreach (var pair in pairs)
{
pair.Compare();
}
}
或者如果您想知道是否都正确
public void CurBtn()
{
foreach (var pair in pairs)
{
if(!pair.Compare())
{
Debug.Log($"{pair.Input} is not correct!");
return;
}
}
Debug.Log("All are correct!");
}