[英]Select an variable using multi variables
我有很多Int32
变量,我想选择要检查的int
。 是否可以使这一行并使用多个变量选择一个变量?
Int32 redleft0 = 0;
Int32 redleft1 = 0;
Int32 redleft2 = 0;
Int32 redleft3 = 0;
Int32 redleft4 = 0;
Int32 redleft5 = 0;
Int32 blueleft0 = 0;
Int32 blueleft1 = 0;
Int32 blueleft2 = 0;
Int32 blueleft3 = 0;
Int32 blueleft4 = 0;
Int32 blueleft5 = 0;
redorblue = "red";
for (int i = 0; i < count; i++)
{
String checkleftint = (redorblue + "left" + i);
if (checkleftint < 0)
{
}
}
您应该在此处使用一个数组-或两个数组:
var red = new int[]{0,0,0,0,0,0};
var blue = new int[]{0,0,0,0,0,0};
var arrayToUse = redorblue == "red" ? red : blue;
for (int i = 0; i < count; i++)
{
var value = arrayToUse[i];
// ....
}
将数组或字典与键和值一起使用,以便可以使用键提取值
这样就行不通了。 要么不使用单独的变量,而是使用数组:
int[,] vars = new int[2,6] { { 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
// int[0,*] would be redleft and int[1,*] would be blueleft
或使用Dictionary<string, int>
如果使用enum ,则是可能的,例如:
enum values { redleft0 = 0; redleft1 = 120; redleft2 = 13; }
通过使用enum
所以,你该做的只是:
for (int i = 0; i < count; i++)
{
if(i == values.redLeft0){
...
}
}
通过使用enum
,可以将所需的名称与所需的数字相关联。
如果这不是您要的内容,请进行说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.