Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C:使用表数组中的名称在循环中创建整数_C# - Fatal编程技术网

C# C:使用表数组中的名称在循环中创建整数

C# C:使用表数组中的名称在循环中创建整数,c#,C#,好的,我试着做我的程序的一部分,只计算一个数字中的所有数字。我擅长将波兰数学参数翻译成英语,我想实现一个循环,在这个循环中创建新的变量,这些变量的名称来自表数组。 下面是我的代码片段: Random rand = new Random(); int[] a = { rand.Next(100,999), rand.Next(100, 999), rand.Next(100, 999), rand.Next(100, 999) }; // random numbers

好的,我试着做我的程序的一部分,只计算一个数字中的所有数字。我擅长将波兰数学参数翻译成英语,我想实现一个循环,在这个循环中创建新的变量,这些变量的名称来自表数组。 下面是我的代码片段:

Random rand = new Random();
        int[] a = { rand.Next(100,999), rand.Next(100, 999), rand.Next(100, 999), rand.Next(100, 999) };    // random numbers
        int j = -1;
        string[] inty = { "k", "l", "m", "n" };    // the array from which i want to extract variable names
        for (int i = 0;i<=a.Length;i++)    // loop
        {
            j++;
            int inty[j] = new int();     // create a new int, where the name of the int is some letter from that array
            Console.WriteLine(a[j]);
        }
        Console.ReadKey();
因此,应该在循环中创建的int应该具有来自该字符串数组inty中的下一个字母的名称。有没有人知道如何做到这一点或以其他方式替换代码

真诚地,
互联网上的一些随机的花花公子。

你可以使用字典:

Random rand = new Random();
int[] a = { rand.Next(100,999), rand.Next(100, 999), rand.Next(100, 999), rand.Next(100, 999) };    // random numbers
string[] inty = { "k", "l", "m", "n" };    
var variables = new Dictionary<string, int>();
for (int i = 0; i<a.Length; i++)
{
    variables.Add(inty[i], a[i]);
    Console.WriteLine(variables[inty[i]]);
}
//example using a variable
Console.WriteLine($"k: {variables["k"]}");
Console.ReadKey();
或:


记住C是强类型的。必须向编译器声明变量。如果希望标识符在IDE中作为变量可用,则必须以这种方式声明它。否则,您只需将名称用作其他类型的数据。

如果我理解您的问题。。。您可以使用模函数,也可以使用哈希集

HashSet<string> names = new HashSet<strings>();

for (int i = 0;i<=a.Length;i++)  
{

 j = j++ % 3; // Will give 0,1,2,0,1,2,0,1,2... or use ranbd.Next(0,4)
 string forName = inty[j]; // Restricted to 0,1,2
 string newName = $"{forName}{a[j]}";
 if (! names.Contains(newName)
 {
  names.Add(newName);
  Console.WriteLine("Name: {newName}");
 }
 else 
 {
  Console.WriteLine("Ups!: {newName} already used!");
 }
}

a中的项目比inty中的项目多,因此即使事情像这样运行,它们也不会!如果a中的最后一项没有变量名,则最终会出现索引超出范围的异常。对,这是我的疏忽,因为我还打算包括n。谢谢,这对我来说非常有效,我认为我需要深入研究一下这些字典,我认为它们可以在更多方面帮助我。过多地使用字典是陷入动态语言思维的一种迹象php、javascript和没有正确接受类型的使用。
HashSet<string> names = new HashSet<strings>();

for (int i = 0;i<=a.Length;i++)  
{

 j = j++ % 3; // Will give 0,1,2,0,1,2,0,1,2... or use ranbd.Next(0,4)
 string forName = inty[j]; // Restricted to 0,1,2
 string newName = $"{forName}{a[j]}";
 if (! names.Contains(newName)
 {
  names.Add(newName);
  Console.WriteLine("Name: {newName}");
 }
 else 
 {
  Console.WriteLine("Ups!: {newName} already used!");
 }
}