C#用唯一的int填充数组No Linq或ArrayLists;

时间:2011-11-11 19:59:08

标签: c# arrays

这段代码很麻烦,但无法弄清楚为什么...想要使用7个独特的随机整数填充数组而不使用arraylists或linq!我知道逻辑不合适......

class Program
{
    static void Main(string[] args)
    {    int current;
         int[] numbers = new int[7];  // size of that array 
         Random rNumber = new Random();
         current = rNumber.Next(1, 50);
         numbers[0] = current;
         Console.WriteLine("current number is {0}", current);
         for (int i=1;i<7;i++)
         {
             current = rNumber.Next(1, 50);
             for (int j = 0; j < numbers.Length; j++)
             {
                 do
                 {
                     if (current == numbers[j])
                     {
                         Console.WriteLine("Duplicate Found");
                         current = rNumber.Next(1, 50);
                     }
                     else
                     {   
                         numbers[j++] = current;
                         break;
                     }
                 }while (current == numbers[j]);

             }//inner for

         }//outer for
         for (int l = 0; l < 7; l++) // DISPLAY NUMBERS
         {
             Console.WriteLine(numbers[l]);
         }

    }// main
 }//class

4 个答案:

答案 0 :(得分:4)

  

想要在不使用的情况下使用7个唯一整数填充数组   arraylists或linq!

int[] list = new int[7];
for (int i = 0; i < list.Length; i++)
{
    list[i] = i;
}

<小时/> 的修改

如果随机数已经在数组中,我改变了你的内循环;创建一个新的随机并将j重置为0.

        for (int i = 1; i < 7; i++)
        {
            current = rNumber.Next(1, 50);
            for (int j = 0; j < numbers.Length; j++)
            {
                if (current == numbers[j])
                {
                    Console.WriteLine("Duplicate Found");
                    current = rNumber.Next(1, 50);
                    j = 0; // reset the index iterator
                }
            }//inner for
            numbers[i] = current; // Store the unique random integer
        }//outer for

答案 1 :(得分:3)

我认为你正在寻找随机数,所以另一个答案并不是你想要的。

这里有几个问题。

  • 内循环正在测试重复项。但是,从0到数组末尾,它正在使用numbers.length。这应该是i,以便与已设置的值进行比较。无论您是否设置了任何元素,numbers.length始终为7。

  • 赋值使用j,因此假定第一个元素不是重复元素,每次都会被覆盖。那应该是numbers[i] = current;。由于for正在处理递增,因此无需++。

  • 如果您确定某个数字是重复的,则应将j重置为zer以再次检查整个列表,而不是将数字放在中间。

如果没有完全重写,更改将如下所示:

     for (int i=1;i<7;i++)
     {
         current = rNumber.Next(1, 50);
         for (int j = 0; j < i; j++)  //----------------- loop through set values
         {
             if (current == numbers[j])
             {
                 Console.WriteLine("Duplicate Found");
                 current = rNumber.Next(1, 50);
                 j = 0; // -----------------------reset the counter to start over
             }
         }//inner for

         // if we got here there is no duplicate --------------------------------
         numbers[i] = current;

     }//outer for

(请注意,我尚未测试此代码,只是添加了更改)

答案 2 :(得分:1)

你继续覆盖else中的相​​同索引,并且还检查太多索引,导致第一个索引始终显示为重复,这是错误的...

将其更改为:

 for (int i=1;i<7;i++)
 {
     current = rNumber.Next(1, 50);

     for (int j = 0; j < i; j++)    ///< change to j < i.  no need to check the others
     {
         do
         {
             if (current == numbers[j])
             {
                 Console.WriteLine("Duplicate Found");
                 current = rNumber.Next(1, 50);
             }
             else
             {   
                 numbers[i] = current;   ///< not j++ but i to prevent writing at the same locations over and over again
                 break;
             }
         }while (current == numbers[j]);
     }//inner for
 }//outer for

答案 3 :(得分:0)

这个怎么样?

int[] list = new int[7]; 
var rn = new Random(Environment.TickCount);
for (int i = 0; i < 7; i++) 
{ 
    var next = rn.Next(1, 50);
    while(Contains(list, next))
    {
        next = rn.Next(1, 50);
    }
    list[i] = next;       
} 


private bool Contains(IEnumerable<int> ints, int num) 
{
    foreach(var i in ints)
    {
        if(i = num) return true;
    }
    return false;
}