我想检查是否满足4个条件

时间:2020-03-31 08:52:01

标签: string unity3d if-statement

我对C#还是陌生的,我正在尝试编写一个代码,该代码将检查我的条件(如果满足4个条件,那么会发生什么情况)。 这是我的代码:enter code here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;
public class NameTrans : MonoBehaviour {
    public string thename;
    public GameObject inputField;
    public GameObject textDisplay;
    public GameObject textDisplay2;

    // Use this for initialization
    public void Showname () {
        thename = inputField.GetComponent<Text>().text;
     if (thename.ToUpper().Contains("Man".ToUpper()) || thename.ToUpper().Contains("Dog".ToUpper()))
        {

            textDisplay2.SetActive(false);
            textDisplay.SetActive(true);
            textDisplay.GetComponent<Text>().text = "WOrks" ;
        }
       else
        {
            textDisplay.SetActive(false);
            textDisplay2.SetActive(true);
            textDisplay2.GetComponent<Text>().text = "Not WORK" ;
        }

            }


}


2 个答案:

答案 0 :(得分:1)

如果仅需要满足4个条件之一,则您已经在做正确的事情。您只需要添加缺少的两个条件。

if((thename.ToUpper().Contains("Man".ToUpper())) || 
   (thename.ToUpper().Contains("Dog".ToUpper())) ||
   (thename.ToUpper().Contains("Cat".ToUpper())) ||       
   (thename.ToUpper().Contains("Fish".ToUpper())))
{
    //do something
}
else
{
    //do something else
}

如果需要同时满足所有四个条件,则需要将条件与“ &&”而不是“ ||”连接起来。

if((thename.ToUpper().Contains("Man".ToUpper())) && 
   (thename.ToUpper().Contains("Dog".ToUpper())) &&
   (thename.ToUpper().Contains("Cat".ToUpper())) &&       
   (thename.ToUpper().Contains("Fish".ToUpper())))
{
    //do something
}
else
{
    //do something else
}

使用新信息更新 如果要检查x条件中是否有4个满足条件,我可能会执行以下操作:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;
public class NameTrans : MonoBehaviour {
    public string thename;
    public GameObject inputField;
    public GameObject textDisplay;
    public GameObject textDisplay2;
    private string[] conditions;
    private int counter;

    // Use this for initialization
    public void Showname () {
     thename = inputField.GetComponent<Text>().text;
     conditions = {"Man", "Dog", "Cat", "Fish", "Goat", 
                   "Frog", "Bird", "Alligator"};
     counter = 0;

     foreach(string cond in conditions)
     {
         if (thename.ToUpper().Contains(cond.ToUpper())
         {
             counter += 1;
             if (counter >= 4)
             {
                 break;   
             }
         }
     }

     if (counter >= 4)
        {

            textDisplay2.SetActive(false);
            textDisplay.SetActive(true);
            textDisplay.GetComponent<Text>().text = "WOrks" ;
        }
       else
        {
            textDisplay.SetActive(false);
            textDisplay2.SetActive(true);
            textDisplay2.GetComponent<Text>().text = "Not WORK" ;
        }

    }


}

答案 1 :(得分:0)

非常感谢您在 Paddex 中输入代码作为答案。 对于使用统一添加新字符串的人[]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {
    public string thename;
    public GameObject inputField;
    public GameObject textDisplay;
    public GameObject textDisplay2;
    private string[] conditions;
    private int counter;
    // Use this for initialization

    public void Showname()
    {
        thename = inputField.GetComponent<Text>().text;
        conditions = new string[] {
            "Man", "Dog", "Cat", "Fish", "Goat", 
                   "Frog", "Bird", "Alligator"};
        counter = 0;
        foreach (string cond in conditions)
        {
            if (thename.ToUpper().Contains(cond.ToUpper()))
            {
                counter += 1;
                if (counter >= 4)
                {
                    break;
                }
            }
        }

        if (counter >= 4)
        {

            textDisplay2.SetActive(false);
            textDisplay.SetActive(true);
            textDisplay.GetComponent<Text>().text = "WOrks";
        }
        else
        {
            textDisplay.SetActive(false);
            textDisplay2.SetActive(true);
            textDisplay2.GetComponent<Text>().text = "Not WORK";
        }

    }


}