如果我声明一个整数:
int house = number;
如何让名称在 forloop 中增加 1(+1 到房子的名称):
例如
int house1 = number[i];
int house2 = number[i];
int house3 = number[i];
你不能。您需要在编译时生成代码。
您应该使用一个数组,而不是一组类似命名的变量。
但是您始终可以使用以下集合:
Dictionary<int, int> houses = new Dictionary<int, int>();
foreach( ... )
houses.Add(i+1, number[i]);
我建议您使用字典
var houses = new Dictionary<string, int>
for(int i=1; int < 10; int++)
{
string houseNumber = "house" + i;
houses[houseNumber] = number[i];
}
然后你可以通过houses["house1"]、houses["house2"]等来引用它们...
您不能增加变量名称。您改为创建一个数组。
int[] houses = {1,2,3,4};
for(int i = 0; i < houses.length; i++){
Console.WriteLine(houses[i]);
}