问题
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;
}
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;
}
Basically it is a script that needs to do different things based on what cropType is in it.
回答1:
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);
}
回答2:
In terms of simplifying a switch
statement, I think a Dictionary
(if keys were not sequential integers), enumeration, or a List
(for something like 1-10
as is the case here) is appropriate, creating a mapped relationship between numbers:
int[] growth = {0, 60, 80, 90, 40, 120, 130, 50, 70, 40, 150};
int cropType = 5; // for example
Console.WriteLine(growth[cropType]); // 120
Here's a dictionary example, which I think is more comprehensible for humans:
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"]);
However, having seen your second switch
statement, it appears your unwieldy switch
es are symptoms of a larger design problem. You may consider adding a Crop
class that has member fields for all of the properties you're manipulating, such as type
and growth
(and any other properties or functions that describe Crop
-ness).
In terms of Global
, you may consider a second class that aggregates Crop
s, such as a Harvest
class with a dictionary that keeps tracks of how much of each crop have been harvested.
Long story short, these design questions can get quite fuzzy and opinion-based, but hopefully this offers some ideas for moving forward.
来源:https://stackoverflow.com/questions/52548196/shorten-long-switch-case