大家好想请教您如何将以下If / else代码转换为for循环。 这是我的代码:
List<int> PrimeBuilders = new List<int>();
int Input;
void OnNextBuild()
{
int Temp = PrimeBuilders.Count;
if(Temp == 2)
{
if(Input == PrimeBuilders[0])
{
do this
}
else if(input == PrimeBuilders[1])
{
do this
}
}
else if(Temp == 3)
{
if(Input == PrimeBuilders[0])
{
do this
}
else if(input == PrimeBuilders[1])
{
do this
}
else if(input == PrimeBuilders[2])
{
do this
}
}
if(Temp == 4)
{
if(Input == PrimeBuilders[0])
{
do this
}
else if(input == PrimeBuilders[1])
{
do this
}
else if(input == PrimeBuilders[2])
{
do this
}
else if(input == PrimeBuilders[3])
{
do this
}
}
}
我想创建一个首先检查temp大小的for循环,然后希望另外一个循环来检查_building_instance.BuildingID是否在List中。然后在获得匹配时运行代码。
非常感谢你们!
答案 0 :(得分:1)
目前还不清楚你要做什么,但你的逻辑可以简化为:
void OnNextBuild()
{
if (PrimeBuilders.Count < 2) return;
int i = PrimeBuilders.IndexOf(_building_instance.BuildingID);
if(i != -1)
{
if(++i == PrimeBuilders.Count) i = 0;
_building_instance = _simulation_manager.
GetCity().
GetBuildingInstance(PrimeBuilders[i]);
_interface_state_manager.SetNextState(InterfaceState.ShowBuildUnitsPanel);
}
}