这段代码用于生成 0 到 1000 之间的 100 个随机数,并显示生成的偶数个数以及最小值、最大值和值的范围。
当我编译时,我收到一个关于使用 minInt 和 maxInt 的未分配局部变量的错误。它应该从生成器中获取分配的变量,我做错了什么?
using System;
using System.Windows.Forms;
namespace ConsoleApplication27
{
class Program
{
static void Main(string[] args)
{
int evenNumbers = 0;
int minInt;
int maxInt;
int range;
string result;
range = maxInt - minInt;
result = "Even numbers:\t" + evenNumbers;
result += "\nMin number:\t" + minInt;
result += "\nMax number:\t" + maxInt;
result += "\nRange of number:\t" + maxInt + " - " + minInt + " = " + range;
DisplayResults(result);
}
static void GenerateNumbers(int evenNumbers, int minInt, int maxInt, int range)
{
//creating 100 element array
//and using the random function to fill it
int[] randomNumbers = new int[100];
Random number = new Random();
int randy;
for (int i = 0; i < randomNumbers.Length; i++)
{
randy = number.Next(1000);
randomNumbers[i] = randy;
if (randy < minInt)
{
minInt = randy;
}
else
if (randy > maxInt)
{
maxInt = randy;
}
if (randomNumbers[i] % 2 == 0)
{
evenNumbers++;
}
}
}
static void DisplayResults(string outcome)
{
MessageBox.Show(
outcome, "results!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}