缩短长开关盒 [英] Shorten long switch case
问题描述
所以让我先说我是 C# 的新手.我有一个 switch 语句,目前有 10 个不同的 case,但是,我需要使用它 3 次不同的时间(相同的 10 个 case,每个 case 的结果不同),并且每个 case 只有一点点变化.
So let me start by saying I am new to C#. I have a switch statement that currently has 10 different cases, however, I need to use it 3 different times (same 10 cases, different results per case), and each case there is only a slight change.
我觉得我只是在重复代码,有没有办法缩短它?
I feel like I'm just repeating code, is there a way to shorten it?
//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 个部分的代码.在第二部分中,我根据情况分配图像,这必须单独完成,因为它依赖于增长和变化,而增长则不然.不过,我实际上并没有在其他开关上使用它.这是我进一步了解的另一个:
That is my code for 1 section. In the 2nd section, I assign an image depending on the case, this has to be done separately as it relies on the growth and changes, whereas growth does not. I don't actually use it on the other swithces though. This is another one I have further down:
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 来做不同的事情.
Basically it is a script that needs to do different things based on what cropType is in it.
推荐答案
也许这太多了,但您可以使用枚举和类/结构加上字典(如 ggorlen 建议)
Maybe this would be too much, but you can use enums and classes/structs plus a dictionary (as ggorlen suggested)
为什么要枚举?避免使用硬编码数字;不易出错并提高可读性;
Why enums? to avoid using hardcoded numbers; less error-prone and will improve readability;
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];
}
在这里(最终)您将检索您想要的值.
Here is where (finally) you will retrieve the values that you want.
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;
}
所以你会以这种方式求值;
So you will ask for values in this way;
private void Example()
{
var carrotsGrowth = GetGrowthFactor(CropType.Carrots);
}
这篇关于缩短长开关盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!