【问题标题】:If statement or for loopif 语句或 for 循环
【发布时间】:2018-07-13 21:37:05
【问题描述】:

每次用户触摸屏幕时,我都会调用一个方法。一旦他们触摸屏幕的次数足够多,脚本所附加的对象就会被停用。

有问题的脚本附加到我根据动态数据循环出来的布局。

假设我有 10 个问题,这意味着我将有 10 个布局和 10 个版本的脚本。

我不确定我是否采取了最好的方法,但我被困住了,因为我是 Unity/C# 的新手,我希望得到一些指导。

我需要脚本根据屏幕索引检查不同的索引。目前,if 语句(在下面的方法中)完美地工作,但是,由于我不知道会有多少屏幕(由于处理动态数据),我需要一种方法来进行这些检查,而不需要大量的 if 列表陈述。

我知道我可以使用 for 循环,但我的问题是我需要根据屏幕索引更改计数(如我的 if 语句中所示)。是否可以使用循环来做我所追求的事情,还是我需要改变我处理这个问题的方式?

我面临的一个问题是,在将新脚本添加到每个实例之间唯一可用的常量是 ScreenIndex 计数变量。因此,为什么我在 if 语句中反复提到这一点。

我尝试将我的方法包装在 for (ScreenIndex = 0; ScreenIndex < UIHandler.GetdynamicQuestionArray().Count; ScreenIndex += 4) 中,但计数不同步。我必须使用嵌套循环吗?我已经尝试过,但到目前为止没有成功。

我的方法目前是这样的:

//Scratch pad 1 - Due to this method being called everytime a pixel is scratched, nestled if statements need to be used.
public void OnEraseProgressScratch1(float progress)
{
    //Set a count of i, to make sure the screen index can be checked
    int i = 0;

    //While loop to help condition to make sure we know what page the user is currently on, keep looping i until it matches the index the user is on.
    //This is necessary because i will keep getting reset everytime this method is called, so the while loop will make sure i matches the index
    while (i != transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex)
    {
        i++;
    }
    //Once the user has scratched more than 50% of the pad, deactivate it
    if (Mathf.Round(progress * 100f) >= 50)
    {
        //If the user has scratched more than 50% stop the user from scratching any further and check if they are correct.
        transform.parent.gameObject.transform.GetChild(0).GetChild(3).GetChild(0).GetChild(0).gameObject.SetActive(false);
        //Loop through the array that holds if an answer is correct or not

        //If the screen index is the same as the page number
        if (transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex == i)
        {
            int ScreenIndex = transform.parent.gameObject.transform.parent.gameObject.GetComponent<TeamQuizScreen>().ScreenIndex;

            if (ScreenIndex == 0)
            {
                Debug.Log("X (should be 0) = " + ScreenIndex);
                //If the user is at screen index 0, then a loop should be querying position 0 only here. If the user is at position 1 then a loop should be 
                //querying position 4 - this is because position 1,2,3 will be queried in 'OnEraseProgressScratch2', 'OnEraseProgressScratch3' and 'OnEraseProgressScratch4'.
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if(ScreenIndex == 1)
            {
                ScreenIndex += 3;

                Debug.Log("X (should be 4) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if (ScreenIndex == 2)
            {
                ScreenIndex += 6;

                Debug.Log("X (should be 8) = " + ScreenIndex);
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            else if (ScreenIndex == 3)
            {
                ScreenIndex += 9;

                Debug.Log("X (should be 12) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            else if (ScreenIndex == 4)
            {
                ScreenIndex += 12;

                Debug.Log("X (should be 16) = " + ScreenIndex);
                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }

            }
            else if (ScreenIndex == 5)
            {
                ScreenIndex += 15;
                Debug.Log("X (should be 20) = " + ScreenIndex);

                if (UIHandler.GetDynamicCorrectAnswerArray()[ScreenIndex].ToString().Contains("Correct"))
                {
                    Debug.Log("Answer 1 is correct");
                }
            }
            //This will need to continue based on the number of questions in the hierarchy
        }
    }
}

【问题讨论】:

  • 我很难理解您要做什么。你能试着让它更简洁吗?
  • 请注意:不需要所有这些 if/else 也不需要永远“需要继续”。看看你的个人情况,你有一个清晰的模式:“X 应该是 = ScreenIndex*4”和ScreenIndex+=(ScreenIndex*3) [不需要括号,但为了清楚起见添加了]。您可以将所有这些缩减为几行。实际上,如果您按照正确的顺序执行它们,它就会变成:ScreenIndex+=ScreenIndex*3; Debug.Log("X should be " + ScreenIndex);... 甚至是 ScreenIndex=ScreenIndex*4ScreenIndex*=4

标签: c# for-loop if-statement unity3d


【解决方案1】:

您可以使用 switch 语句进行检查。

switch (ScreenIndex)
{
    case 0:
        Debug.Log ("X (should be 0) = " + ScreenIndex);
        //If the user is at screen index 0, then a loop should be querying position 0 only here. If the user is at position 1 then a loop should be 
        //querying position 4 - this is because position 1,2,3 will be queried in 'OnEraseProgressScratch2', 'OnEraseProgressScratch3' and 'OnEraseProgressScratch4'.
        if (UIHandler.GetDynamicCorrectAnswerArray () [ScreenIndex].ToString ().Contains ("Correct"))
        {
            Debug.Log ("Answer 1 is correct");
        }
    break;
    case 1:
        ScreenIndex += 3;

        Debug.Log ("X (should be 4) = " + ScreenIndex);

        if (UIHandler.GetDynamicCorrectAnswerArray () [ScreenIndex].ToString ().Contains ("Correct"))
        {
            Debug.Log ("Answer 1 is correct");
        }
    break; //... and so on

或者...你可以换一种方法。

public void Method (int screenIndex, int AddToIndex)
{
    Debug.LogFormat ("X (should be {0}) = {1}", ScreenIndex, ScreenIndex + AddToIndex);
    ScreenIndex += AddToIndex

    if (UIHandler.GetDynamicCorrectAnswerArray () [ScreenIndex].ToString ().Contains ("Correct"))
    {
        Debug.Log ("Answer 1 is correct");
    }
}

只需为您的方法制定一个公式,例如

int toAdd = ScreenIndex * 3;

【讨论】:

  • 刚刚设法在我的笔记本电脑上查看了您的答案(最初,我在手机上查看),我可以看到您关于该方法的建议确实有效!感谢您的帮助!
猜你喜欢
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2019-06-09
相关资源
最近更新 更多