2013-03-19 25 views
1

我有很多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) 
    { 
    } 
} 
+2

或者使用byte []/int [],因为它似乎适合你想要做得更好的东西。 – kape123 2013-03-19 13:16:05

+2

请使用数组。 – 2013-03-19 13:16:58

回答

7

您应该使用数组 - 或者说2 - 在这里:

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]; 
    // .... 
} 
+0

'+ 1'为正确的方向.. – 2013-03-19 13:19:33

+0

谢谢我只是改变代码,然后到阵列的 – Kapein 2013-03-19 13:19:48

+0

这不仅帮助这个问题,但也有助于未来的问题! – Kapein 2013-03-19 13:23:45

0

使用数组或一个Dictonary与键和值,所以你可以拉出来使用密钥

0

它不会那样工作。要么不使用独立的变量,但使用数组:

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>

-1

如果使用enum,例如这是可能的:

enum values { redleft0 = 0; redleft1 = 120; redleft2 = 13; } 

通过使用enum

所以你wolud做的仅仅是:

for (int i = 0; i < count; i++) 
{ 
    if(i == values.redLeft0){ 
    ... 
    } 
} 

使用enum,你要在你想名称关联。

如果这不是你问什么,请澄清。

+0

你可以设置枚举值吗? – CR41G14 2013-03-19 13:17:24

+0

@ CR41G14:没有看到任何有问题的证据需要设置这些值。我看到OP想检查他们。 – Tigran 2013-03-19 13:19:21