C# 用唯一的整数填充数组 没有 Linq 或 ArrayList;

发布于 2024-12-15 01:51:59 字数 1234 浏览 0 评论 0原文

这段代码有错误,但无法弄清楚为什么......想要用 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

This code is buggy but can't figure out why ... want to populate an array with 7 unique random integers without using arraylists or linq! I know the logic is not okay...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻安 2024-12-22 01:51:59

想要用 7 个唯一整数填充数组而不使用
数组列表或 linq!

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


EDIT

如果随机数已经在数组中,我改变了你的内部循环;创建一个新的随机数并将 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

want to populate an array with 7 unique integers without using
arraylists or linq!

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


EDIT

I changed your inner loop, if the random number is already in the array; create a new random and reset j to 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
老子叫无熙 2024-12-22 01:51:59

我假设您正在寻找随机数,因此另一个答案不是您正在寻找的。

这里有几个问题。

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

  • 赋值使用j,所以假设第一个元素不是重复的,它每次都会被覆盖。这应该是numbers[i] = current;。不需要 ++,因为 for 正在处理递增。

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

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

     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

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

I presume you are looking for random numbers, so the other answer is not what you are looking for.

There are a couple of issues here.

  • The inner loop is testing for duplicates. However, it is looking from 0 through the end of the array since it is using numbers.length. This should probably be i, to compare with already set values. numbers.length is always 7 regardless of whether or not you set any of the elements.

  • the assignment is using j, so presuming the first element is not a duplicate, it will be overwritten each time. That should be numbers[i] = current;. No ++ necessary as the for is handling the incrementing.

  • if you determine that a number is a duplicate, j should be reset to zer to check against the entire list again rather than having the while in the middle.

Without a complete rewrite, the changes will look something like this:

     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

(Please note that I have not tested this code, just added the changes)

无语# 2024-12-22 01:51:59

您不断覆盖 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

you keep overwriting the same indexes in the else, and also checking too many indices causing the first to show up as a duplicate at all times which was false...

change it to:

 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
情定在深秋 2024-12-22 01:51:59

这又如何呢?

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;
} 

What about this?

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;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文