1

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

1 回答 1

1

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

int randy;
for (int i = 0; i < randomNumbers.Length; i++)
{
    randy = number.Next(1000);
    randomNumbers[i] = randy

编辑:我很惊讶这甚至可以编译。它不应该抱怨尝试使用未初始化的变量吗?

编辑:此外,您需要为您的方法分配一些价值。在这些修复之后,它应该编译(但不会给你你需要的结果,因为你没有调用或收集结果。为此你应该使用:minIntmaxIntMainGenerateNumbersref

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 > 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++;
    }
}
于 2012-10-15T20:25:37.643 回答