如何在回答并单击播放按钮后逐一显示问题

How to make questions displayed one-by-one after answered and clicked to Play button

我是Unity新手,尝试在回答并点击播放按钮后逐一显示问题

public class PlayGame : MonoBehaviour
{
 
    public string[] questions = {"What is 10+10", "What is 20+20", "What is 30+30", "What is 40+40", "What is 50+50"};
    public string[] correctAnswer = {"20", "40", "60", "80" , "100"};

    public Text question;
    public InputField answer;
    public int selection;


    // Start is called before the first frame update
    void Start()
    {
     question.text = questions[selection];      
    }


    public void CheckAnswer()
    {
        if (answer.text == correctAnswer.ToString())            
        {
            Debug.Log("Answer is Correct");
         //display next question
            
        }
        else
        {
            Debug.Log("Answer is not Correct");
            //display next question
        }
    }
}

这应该很简单

private int selection = -1;

void Start()
{
    ShowNextQuestion();     
}

private void ShowNextQuestion()
{
    selection++;
    if(selection >= questions.Length - 1) 
    {
        // You said you wanted to restart the questions so simply do
        selection = 0;
    }

    question.text = questions[selection];
}

public void CheckAnswer()
{
    if (answer.text.Equas(correctAnswer[selection]))            
    {
        Debug.Log("Answer is Correct");
    }
    else
    {
        Debug.Log("Answer is not Correct");
    }

    ShowNextQuestion();
}

让我告诉你,一般来说,将问题和答案存储在两个单独的数组中并不是一个好主意。

  • 你不能真正确定两个单独的数组总是具有相同的长度
  • 在中间添加或删除问题时你有双重工作
  • 想象一下以后想要随机化问题的顺序.. 你不知道哪个答案属于哪个问题

所以我建议你更愿意将它们以类似

的类型结合在一起
[Serializable]
public class Question
{
    // Allow to edit these in the Inspector but nowhere else
    [SerializeField] private string _text;
    [SerializeField] private string _answer;

    // Other classes may only read these
    public string Text => _text;
    public string Answer => _answer;

    // Constructor 
    public Question(string text, string answer)
    {
        _text = text;
        _answer = answer;
    }
} 

现在在您的组件中,您宁愿通过 Inspector 设置它们或通过

初始化它们
public Question[] questions = {
    new Question("What is 10+10", "20"),
    new Question("What is 20+20", "40"),
    new Question("What is 30+30", "60"),
    new Question("What is 40+40", "80"),
    new Question("What is 50+50", "100")
};

然后你当然相应地更改代码以访问这些

private void ShowNextQuestion()
{
    selection++;
    if(selection >= questions.Length - 1) 
    {
        // You said you wanted to restart the questions so simply do
        selection = 0;
    }

    question.text = questions[selection].Text;
}

public void CheckAnswer()
{
    if (answer.text.Equals(questions[selection].Answer))           
    {
        Debug.Log("Answer is Correct");
    }
    else
    {
        Debug.Log("Answer is not Correct");
    }

    ShowNextQuestion();
}

如上所述,这还可以通过在开始前随机排列问题来为您的应用程序提供一点随机性:

using System.Linq;

...

private void Start()
{
    questions = questions.OrderBy(q => Random.value).ToArray();

    ShowNextQuestion();
}

private void ShowNextQuestion()
{
    selection++;
    if(selection >= questions.Length - 1) 
    {
        // You said you wanted to restart the questions so simply do
        selection = 0;
        questions = questions.OrderBy(q => Random.value).ToArray();
    }

    question.text = questions[selection].Text;
}