【问题标题】:How to compare elements of input field text array with another array of int?如何将输入字段文本数组的元素与另一个 int 数组进行比较?
【发布时间】:2019-01-07 18:21:51
【问题描述】:

我想创建一个包含 10 个输入字段的数组来获取用户输入,然后与另一个 int 数组元素进行比较。 我无法理解如何调用输入字段子文本元素。 任何人都可以指导我使用c#统一实现这一目标的最佳方法吗?谢谢 我正在尝试显示 10 个问题,用户必须通过在输入字段中输入文本来回答这些问题。我想制作输入字段数组来存储用户的答案和另一个数组来存储正确的答案。然后我想在用户点击检查按钮时比较两者。如果答案是正确的,我会用绿色或红色突出显示它。

public class YouTryTables : MonoBehaviour{

int n = 1;
public Text x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
public int ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9, ans10;
public InputField[] allInputFields = new InputField[10]; //array of user Answers entered in input fields
public int[] allAnswers = new int[10];//array of correct answers

public void Start()
{

}

public void GetInput1(string i)
{
}

public void GenerateTable(int n)
{
    x1.text = (n + "  X  " + 1 + "    = ").ToString();
    x2.text = (n + "  X  " + 2 + "    = ").ToString();
    x3.text = (n + "  X  " + 3 + "    = ").ToString();
    x4.text = (n + "  X  " + 4 + "    = ").ToString();
    x5.text = (n + "  X  " + 5 + "    = ").ToString();
    x6.text = (n + "  X  " + 6 + "    = ").ToString();
    x7.text = (n + "  X  " + 7 + "    = ").ToString();
    x8.text = (n + "  X  " + 8 + "    = ").ToString();
    x9.text = (n + "  X  " + 9 + "    = ").ToString();
    x10.text = (n + "  X  " + 10 + "  = ").ToString();


    for (int i = 0; i < allInputFields.Length; i++)
    {
        GameObject obj = GameObject.Find("MyObjectWithInputField");

        allInputFields[i] = obj.GetComponent<InputField>();
    }

    for (int j = 0; j< allAnswers.Length; j++)
    {

        allAnswers[j] = ans1; 
    }

    ans1 = (n * 1);
    ans2 = (n * 2);
    ans3 = (n * 3);
    ans4 = (n * 4);
    ans5 = (n * 5);
    ans6 = (n * 6);
    ans7 = (n * 7);
    ans8 = (n * 8);
    ans9 = (n * 9);
    ans10 = (n * 10);
}

public void ComaprAnswers()
{

    if (allInputFields[i] == allAnswers[j])
    {
        Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
        text.color = Color.green;
    }
    else
    {
        Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
        text.color = Color.red;
    }
}

}

谢谢

【问题讨论】:

  • 请更具体,以获得更具体的答案。添加您已完成的操作的屏幕截图并说明您正在尝试执行的操作。

标签: c# unity3d


【解决方案1】:

为了使用 InputField,您需要 UI 命名空间:using UnityEngine.UI;:

// Find GameObject with InputField component attached
GameObject obj = GameObject.Find("MyObjectWithInputField");

// Get the InputField component from the object
InputField inputField = obj.GetComponent<InputField>();

// Read the input value of the InputField
string text = inputField.text;

我不确定您要进行哪种比较,但您只需遍历 InputFields 并读取如上所示的值。要获取场景中的所有 InputFields,您可以使用

InputField[] allInputFields = FindObjectsOfType<InputField>();

编辑:

在 OP 中编辑的新代码,这里有一个快速注释和小修改来解释代码中的一些错误。检查我添加的 TODO:s。

public class YouTryTables : MonoBehaviour 
{
    // TODO: This variable isn't used
    int n = 1;
    public Text x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
    // TODO: These variables aren't used
    public int ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9, ans10;
    public InputField[] allInputFields = new InputField[10]; //array of user Answers entered in input fields
    public int[] allAnswers = new int[10];//array of correct answers

    public void GenerateTable(int n)
    {
        // TODO: Doesn't need .ToString();, they're already strings
        x1.text = (n + "  X  " + 1 + "    = ").ToString();
        x2.text = (n + "  X  " + 2 + "    = ").ToString();
        x3.text = (n + "  X  " + 3 + "    = ").ToString();
        x4.text = (n + "  X  " + 4 + "    = ").ToString();
        x5.text = (n + "  X  " + 5 + "    = ").ToString();
        x6.text = (n + "  X  " + 6 + "    = ").ToString();
        x7.text = (n + "  X  " + 7 + "    = ").ToString();
        x8.text = (n + "  X  " + 8 + "    = ").ToString();
        x9.text = (n + "  X  " + 9 + "    = ").ToString();
        x10.text = (n + "  X  " + 10 + "  = ").ToString();



        for (int i = 0; i < allInputFields.Length; i++)
        {
            // You loop though all 10 arrays but you assign the same component every time; the one on the GameObject you call MyObjectWithInputField
            // You need to get the InputField from the correct GameObjects. Maybe you want to do this in a `public GameObject inputGameObjects` instead?
            // TODO: Get InputField from correct GameObject
            GameObject obj = GameObject.Find("MyObjectWithInputField");
            allInputFields[i] = obj.GetComponent<InputField>();
        }

        for (int j = 0; j < allAnswers.Length; j++)
        {
            // TODO: ans1 isn't set yet, but you don't need to save it to a variable first, you can
            // simply do allAnswers[j] = n * (j + 1);
            allAnswers[j] = ans1; 
        }

        // TODO: You can remove this if you did allAnswers[j] = n * (j + 1); above
        ans1 = (n * 1);
        ans2 = (n * 2);
        ans3 = (n * 3);
        ans4 = (n * 4);
        ans5 = (n * 5);
        ans6 = (n * 6);
        ans7 = (n * 7);
        ans8 = (n * 8);
        ans9 = (n * 9);
        ans10 = (n * 10);
    }

    // TODO: Typo Comapr -> Compare
    // I changed this method to return a bool if all answers were correct
    public bool ComaprAnswers()
    {
        bool allAnswersCorrect = true;

        // TODO: You want to loop through all questions here
        // for (int i = 0; i < allInputFields.Length; i++)
        // TODO: You're comparing string to int, allAnswers should be set to string[] and its setters be made .ToString()
        if (allInputFields[i] == allAnswers[j])
        {
            Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
            text.color = Color.green;
        }
        else
        {
            Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
            text.color = Color.red;
            allAnswersCorrect = false;
        }

        return allAnswersCorrect;
    }
}

编辑2

看到截图后,这是你得到答案值的方式:

var answerParent = GameObject.Find("AnswerPanel");
var answerObject = answerParent.trasnform.GetChild(n); // Where n is 0 - 9
var answerValue = answerObject.GetComponent<InputField>().text;

【讨论】:

  • 此外,由于 OP 要求 compare with another int array,您还需要解析为 int,例如使用Int32.TryParse(inputField.text, out x); (more info)
  • @Maakep 抱歉没有详细解释我的问题。我正在尝试显示 10 个问题,用户必须通过在输入字段中输入文本来回答这些问题。我想制作输入字段数组来存储用户的答案和另一个数组来存储正确的答案。然后我想在用户点击检查按钮时比较两者。如果答案是正确的,我会用绿色或红色突出显示它。
  • 我正在更新我的代码,该代码目前有很多错误,只是试图展示我想要实现的目标。希望你能指导我。非常感谢你
  • 添加代码很棒,但是您能否还包括一个屏幕截图来解释您如何设置游戏对象。就像,在层次结构中;游戏对象如何放置以及哪些游戏对象上有哪些组件。恐怕我需要它来帮助你。如果没有,我可以指出您代码中的所有错误以及您需要做些什么来修复它们。
  • 我编辑了我的帖子以包含对您的代码的评论,@Sarita
【解决方案2】:

var ret = FirstArray.Any(x=> SecondArray.Any(y=>x==y))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多