Как сравнить элементы текстового массива поля ввода с другим массивом int? - PullRequest
0 голосов
/ 03 января 2019

enter image description here Я хочу создать массив из 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;
    }
}

}

Спасибо

Ответы [ 2 ]

0 голосов
/ 03 января 2019

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

0 голосов
/ 03 января 2019

Для использования InputField вам понадобится пространство имен пользовательского интерфейса: 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 и читаете значения, как показано выше. Чтобы получить все поля ввода в сцене, вы можете использовать

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

EDIT:

Новый код, отредактированный в OP, вот небольшая аннотация и незначительные правки для объяснения некоторых ошибок в коде. Проверьте TODO: я добавил.

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;
    }
}

EDIT2

После просмотра скриншота вы получите значения ответа:

var answerParent = GameObject.Find("AnswerPanel");
var answerObject = answerParent.trasnform.GetChild(n); // Where n is 0 - 9
var answerValue = answerObject.GetComponent<InputField>().text;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...