缩短长开关盒

时间:2018-09-28 04:15:46

标签: c# unity3d switch-statement

因此,首先让我说我是C#的新手。我有一个switch语句,当前有10种不同的情况,但是,我需要使用3次不同的时间(相同的10种情况,每种情况下会有不同的结果),并且每种情况都只有微小的变化。

我觉得我只是在重复代码,有什么办法可以缩短它?

//Set the growth time of the crop based on what cropType.
        switch (cropType) {
            case 1:
                //Potatoes
                growth = 60;
                break;
            case 2:
                //Strawberries
                growth = 80;
                break;
            case 3:
                //Cabbages
                growth = 90;
                break;
            case 4:
                //Carrots
                growth = 40;
                break;
            case 5:
                //Melon
                growth = 120;
                break;
            case 6:
                //Pumpkin
                growth = 130;
                break;
            case 7:
                //Eggplant
                growth = 50;
                break;
            case 8:
                //Mushroom
                growth = 70;
                break;
            case 9:
                //Wheat
                growth = 40;
                break;
            case 10:
                //Truffle
                growth = 150;
                break;
        }

这是我1节的代码。在第二部分中,我根据情况分配图像,这必须单独进行,因为它取决于增长和变化,而增长不是。我实际上并没有在其他开关上使用它。这是我要深入探讨的另一件事:

switch (cropType) {
            case 1:
                //Potatoes
                Debug.Log("Potatoes Harvested!");
                Global.potato += 2;
                break;
            case 2:
                //Strawberries
                Debug.Log("Strawberries Harvested!");
                Global.strawberry += 4;
                break;
            case 3:
                //Cabbages
                Debug.Log("Cabbages Harvested!");
                Global.cabbage += 1;
                break;
            case 4:
                //Carrots
                Debug.Log("Carrots Harvested!");
                Global.carrot += 3;
                break;
            case 5:
                //Melon
                Debug.Log("Melons Harvested!");
                Global.melon += 1;
                break;
            case 6:
                //Pumpkin
                Debug.Log("Pumpkins Harvested!");
                Global.pumpkin += 1;
                break;
            case 7:
                //Eggplant
                Debug.Log("Eggplant Harvested!");
                Global.eggplant += 2;
                break;
            case 8:
                //Mushroom
                Debug.Log("Mushrooms Harvested!");
                Global.mushroom += 4;
                break;
            case 9:
                //Wheat
                Debug.Log("Wheat Harvested!");
                Global.wheat += 6;
                break;
            case 10:
                //Truffle
                Debug.Log("Truffles Harvested!");
                Global.truffle += 1;
                break;
        }

基本上,它是一个脚本,需要根据其中的cropType进行不同的操作。

2 个答案:

答案 0 :(得分:3)

就简化switch语句而言,我认为Dictionary(如果键不是顺序整数),枚举或List(对于1-10之类的东西(在这种情况下)是合适的,在数字之间创建映射关系:

int[] growth = {0, 60, 80, 90, 40, 120, 130, 50, 70, 40, 150};
int cropType = 5; // for example
Console.WriteLine(growth[cropType]); // 120

这是一个字典示例,我认为它对于人类来说更容易理解:

Dictionary<string, int> growth = new Dictionary<string, int>()
{
    {"Potatoes", 60}, 
    {"Strawberries", 80}, 
    {"Cabbages", 90}, 
    {"Carrots", 40}, 
    {"Melon", 120}, 
    {"Pumpkin", 130}, 
    {"Eggplant", 50}, 
    {"Mushroom", 70}, 
    {"Wheat", 70}, 
    {"Truffle", 150} 
};

Console.WriteLine(growth["Melon"]);

但是,看到第二条switch语句后,看来笨拙的switch是较大设计问题的症状。您可以考虑添加一个Crop类,该类具有要处理的所有属性的成员字段,例如typegrowth(以及描述{{1 }}-ness)。

Crop而言,您可以考虑使用第二类来汇总Global,例如带有字典的Crop类,该字典可以记录每种作物的产量收获。

长话短说,这些设计问题会变得非常模糊和基于意见,但是希望这会为前进提供一些思路。

答案 1 :(得分:1)

也许这太多了,但是您可以使用枚举和类/结构以及字典(如ggorlen建议)

为什么要枚举?避免使用硬编码数字;易于出错,可提高可读性;

private enum CropType
{
    Undefined = 0,
    Cabbages,
    Carrots,
    Eggplant,
    Melon,
    Mushroom,
    Potatoes,
    Pumpkin,
    Strawberries,
    Truffle,
    Wheat
}

private struct Crop
{
    public CropType Type { get; private set; }
    public float GrowthFactor { get; private set; }
    public float HarvestFactor { get; private set; }

    public Crop(CropType type, float growthFactor, float harvestFactor) 
    {
        this.Type = type;
        this.GrowthFactor = growthFactor;
        this.HarvestFactor = harvestFactor;
    }
}

private Dictionary<CropType, Crop> crops;
private Dictionary<CropType, Crop> Crops 
{
    get 
    {
        if (crops == null) 
        {
            crops = new Dictionary<CropType, Crop>() 
            {
                { CropType.Cabbages, new Crop(CropType.Cabbages, 90, 1) },
                { CropType.Carrots, new Crop(CropType.Carrots, 80, 5) }
                // here you can add the rest of your products...
            };
        }
        return crops;
    }
}

public Crop GetCrop(CropType crop) 
{
    if (!Crops.ContainsKey(type)) 
    {
        Debug.LogWarningFormat("GetCrop; CropType [{0}] not present in dictionary ", type);
        return null;
    }

    return Crops[type];
}

在这里(最后)您将在其中检索所需的值。

public float GetGrowthFactor(CropType type) 
{
    var crop = GetCrop(type);
    return crop == null ? default(float) : crop.GrowthFactor;
}

public float GetHarvestFactor(CropType type) 
{
    var crop = GetCrop(type);
    return crop == null ? default(float) : crop.HarvestFactor;
}

因此您将以这种方式要求输入值;

private void Example()
{
    var carrotsGrowth = GetGrowthFactor(CropType.Carrots);
}