编译时发生未分配的变量

时间:2012-10-15 20:22:09

标签: c#

这段代码用于生成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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我怀疑你的主要问题是你从未将随机数分配给randy

int randy;
for (int i = 0; i < randomNumbers.Length; i++)
{
    randy = number.Next(1000);
    randomNumbers[i] = randy
编辑:我很惊讶这甚至编译。难道它不应该抱怨尝试使用非初始化变量吗?

编辑:此外,您需要在minInt方法中将某些值分配给maxIntMain。在这些修复之后,它应该编译(但不会提供您需要的结果,因为您不调用GenerateNumbers或从中收集结果。为此,您应该使用ref

int evenNumbers = 0;
int minInt = Int32.MaxValue;
int maxInt = Int32.MinValue;
int range;
string result;

GenerateNumbers(ref evenNumbers, ref minInt, ref maxInt);

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

然后是您的GenerateNumbers方法:

static void GenerateNumbers(ref int evenNumbers,  ref int minInt,  ref int maxInt)
{
    //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++;
        }
    }
}

(我取出range参数,因为它没有被使用)

另请注意,我最初将minIntmaxInt变量设置为最大/最小值。这意味着他们将获取第一个随机值,然后保持更新。

编辑:稍微清理了生成器代码。请注意if < elseif >检查,因为存在极端情况条件,如果随机数太少且范围太小,或者生成的每个随机数都在下降,那么你的“else if(randomNumber&gt)是合理的; maxInt“检查从不执行。

static void GenerateNumbers(ref int evenNumbers,  ref int minInt,  ref int maxInt)
{
    Random numberGenerator = new Random();
    for (int i = 0; i < 100; i++)
    {
        int randomNumber = numberGenerator.Next(1000);

        if (randomNumber < minInt)
            minInt = randomNumber;

        if (randomNumber > maxInt)
            maxInt = randomNumber;

        if (randomNumber % 2 == 0)
            evenNumbers++;
    }
}