[英]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.