C# 其他字符串名称的字符串名称
我需要设置一个新字符串,用户需要设置这个新字符串名 例如:C# 其他字符串名称的字符串名称,c#,C#,我需要设置一个新字符串,用户需要设置这个新字符串名 例如: int input = 0; while (input != -1) { input = int.Parse(Console.ReadLine()); int count = 0; count ++; int ("the " count)= intput;
int input = 0;
while (input != -1)
{
input = int.Parse(Console.ReadLine());
int count = 0;
count ++;
int ("the " count)= intput;
}
我想,也就是说,你们需要什么:
int input = 0;
var inputs = new List<int>();
while (input != -1)
{
input = int.Parse(Console.ReadLine());
int count = 0;
count++;
inputs.Add(input);
}
var result = inputs.Select((j, i) => Tuple.Create("The " + i, j)).ToList();
int输入=0;
变量输入=新列表();
while(输入!=-1)
{
input=int.Parse(Console.ReadLine());
整数计数=0;
计数++;
输入。添加(输入);
}
var result=inputs.Select((j,i)=>Tuple.Create(“+i,j)).ToList();
你不要“动态变量”,你要的是字典。大概是这样的:
Dictionary<int, int> values = new Dictionary<int, int>();
int count = 0;
int input = 0;
while (input != -1)
{
input = int.Parse(Console.ReadLine());
count++;
values.Add(count, input);
}
字典值=新字典();
整数计数=0;
int输入=0;
while(输入!=-1)
{
input=int.Parse(Console.ReadLine());
计数++;
值。添加(计数、输入);
}
如果需要,您可以在某些输出中执行“部分”
,但在本例中存储的值似乎只是int
值。如果您想获得创造性,可以将一些数据类型包装到自定义类中,并从该类中轻松输出格式化字符串
上面要注意的另一个变化是将count
的声明移到循环之外,否则它将始终为1
基本上,每当您发现自己想要一个遵循某种模式的动态变量名时,您实际上想要的是某种类型的集合。可能是一本字典,可能是一个列表,可能是其他东西,等等。用户将永远需要输入值。你的意思是什么?我不知道从哪里开始。每次迭代时设置为0然后设置为1的count变量,或者将字符串声明为变量名,并为其分配intput值。还有,这到底是怎么回事?谢谢你帮了我很多忙你帮了我很多忙