C# 用唯一的整数填充数组 没有 Linq 或 ArrayList;
这段代码有错误,但无法弄清楚为什么......想要用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
EDIT
如果随机数已经在数组中,我改变了你的内部循环;创建一个新的随机数并将 j 重置为 0。
EDIT
I changed your inner loop, if the random number is already in the array; create a new random and reset j to 0.
我假设您正在寻找随机数,因此另一个答案不是您正在寻找的。
这里有几个问题。
内部循环正在测试重复项。但是,由于它使用的是
numbers.length
,因此它会从 0 开始查找到数组末尾。这可能应该是i
,以便与已设置的值进行比较。无论您是否设置任何元素,numbers.length
始终为 7。赋值使用j,所以假设第一个元素不是重复的,它每次都会被覆盖。这应该是
numbers[i] = current;
。不需要 ++,因为for
正在处理递增。如果您确定某个数字是重复的,则应将 j 重置为 zer 以再次检查整个列表,而不是将 while 放在中间。
如果没有完全重写,更改将如下所示:(
请注意,我尚未测试此代码,只是添加了更改)
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 bei
, 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 thefor
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:
(Please note that I have not tested this code, just added the changes)
您不断覆盖 else 中的相同索引,并且还检查太多索引,导致第一个索引始终显示为重复项,这是错误的...
将其更改为:
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:
这又如何呢?
What about this?