我正在使用C#控制台应用程序开展足球联赛项目。 我试图从阵列中选择随机球队,其中包含在主场和客场比赛的球队。 当我试图生成9个随机数时,只生成8个数而0不生成,所以代码不能打破for循环。 我想问题是if语句不允许生成相同的数字和int数组元素'默认值为0.
这是代码和输出:
int randomHome; //Random number genetator for choosing a random iteration value from home array which containss the teams which plays at their home
int randomAway; //Random number genetator for choosing a random iteration value from awayarray which containss the teams which plays at away
Random randomNum = new Random();
int[] randomHomeNumArray = new int[home.Length]; //array will hold the randomHome values and home array is the array which is holding the team's iteration values which plays at their home
int[] randomAwayNumArray = new int[away.Length]; //array will hold the randomAway values and away array is the array which is holding the team's iteration values which plays at away
for (int homeArrayCounter = 0; homeArrayCounter < randomHomeNumArray.Length; homeArrayCounter++)
{
randomHome = randomNum.Next(home.Length)
if (!randomHomeNumArray.Contains(randomHome) )
{
randomHomeNumArray[homeArrayCounter] = randomHome; //It will hold the randomHome values
Console.WriteLine(homeArrayCounter + ". iterasyon in Home " + randomHomeNumArray[homeArrayCounter]);
}
else
{
homeArrayCounter--;
}
}
Console.WriteLine("\n\n");
for (int awayArrayCounter = 0; awayArrayCounter < randomAwayNumArray.Length; awayArrayCounter++)
{
randomAway = randomNum.Next(randomAwayNumArray.Length);
if (!randomAwayNumArray.Contains(randomAway))
{
randomAwayNumArray[awayArrayCounter] = randomAway; //It holds the random valures from away array which contains the teams which plays at away
Console.WriteLine(awayArrayCounter + ". iterasyon in Away " + randomAwayNumArray[awayArrayCounter]);
}
else
{
awayArrayCounter--;
}
}
答案 0 :(得分:3)
当您初始化数组时,默认情况下每个索引的值为0。当您使用随机数时,它总是跳过0,因为它已经存在。
您可以尝试这样: -
for(int i= 0; i<randomHomeNumArray.Length; i++){
randomHomeNumArray[i] = -1;
}
for (int homeArrayCounter = 0; homeArrayCounter < randomHomeNumArray.Length; homeArrayCounter++)
{
do{
randomHome = randomNum.Next(home.Length);
} while(!randomHomeNumArray.Contains(randomHome));
randomHomeNumArray[homeArrayCounter] = randomHome; //It will hold the randomHome values
Console.WriteLine(homeArrayCounter + ". iterasyon in Home " + randomHomeNumArray[homeArrayCounter]);
}
答案 1 :(得分:2)
您似乎正在尝试随机化数组。
请改为尝试:
Random randomNum = new Random();
int[] randomHomeNumArray = Enumerable.Range(0, home.Length).OrderBy(_ => randomNum.Next()).ToArray();
int[] randomAwayNumArray = Enumerable.Range(0, away.Length).OrderBy(_ => randomNum.Next()).ToArray();
那就是它。完成。
答案 2 :(得分:1)
您的问题是数组的默认初始化:
int[] randomHomeNumArray = new int[home.Length];
这会创建一个充满0
的数组,因为0
是int
的默认值。
那么你的if
条件
if (!randomHomeNumArray.Contains(randomHome))
对于false
,始终为0
,因为0
已包含在数组中。
您可以像这样初始化数组:
int[] randomHomeNumArray = Enumerable.Repeat(-1, home.Length).ToArray();
因此,请使用-1
代替0
,而if
条件有效。
答案 3 :(得分:0)
由于int
不是无法使用的数据类型,因此默认情况下会使用零初始化int[]
。所以即使你认为它是一个空数组,它实际上是一个所有元素都设置为零的数组。
要解决此问题,您可以考虑使用int?[]
(null-able int数组)。或者,您可以使用负整数或大于最大包含上限的某个整数初始化数组。更好的是,以更好的方式实现您想要的,使用 @Enigmativity 提供的解决方案,并标记他的答案,如果有帮助的话。