C#在使用全局变量时遇到问题

时间:2017-11-08 14:15:14

标签: c# arrays function for-loop procedure

所以,我已经完成了大部分代码,但有一个标准要求我使用过程。所以,当我决定使用一个程序时,我遇到了几个问题。主要问题是我在第一种方法(Generate Numbernumber)中包含的变量不会出现在我的其他方法中。所以我可以将这些变量设置为全局,以便它们可以在所有方法中工作我的方法还是有另一种解决这个问题的方法吗?

注意:这不是我的完整代码,只是它的一部分。

class Program
{
    static void Main(string[] args)
    {
        Random GenerateNumber = new Random();
        int[] number = new int[6];
        Generating();
        Ordering();
    }

    static void Generating()
    {
        Console.Clear();
        Console.WriteLine("Stage 1 : 6 random numbers have been generated:\n");
        for (int c = 0; c < number.Length; c++)
        {
            if (number[c] == 0)
            {
                number[c] = GenerateNumber.Next(1, 50);
                Console.Write("Random number " + (c + 1) + " = " + number[c] + "\n");
            }
        }
    }

    static void Ordering()
    {
        Console.Clear();
        for (int i = 0; i < number.Length; i++)
        {
            Array.Sort(number);
            Console.Write("Number " + (i + 1) + " = " + number[i] + "\n");
        }
    }
}

3 个答案:

答案 0 :(得分:2)

让它们变得全球化是最简单的,但如上所述,并非最好的方法。

class Program
{
    static Random GenerateNumber = new Random();
    static int[] number = new int[6];

    static void Main(string[] args)
    {
        Generating();
        Ordering();
    }

最好使用参数

class Program
{    
    static void Main(string[] args)
    {
        Random GenerateNumber = new Random();
        int[] number = new int[6];
        Generating(GenerateNumber, number);
        Ordering(number);
    }

    static void Generating(Random generateNumber, int[] number)
    {
        Console.Clear();
        Console.WriteLine("Stage 1 : 6 random numbers have been generated:\n");
        for (int c = 0; c < number.Length; c++)
        {
            if (number[c] == 0)
            {
                number[c] = generateNumber.Next(1, 50);
                Console.Write("Random number " + (c + 1) + " = " + number[c] + "\n");
            }
        }
    }

    static void Ordering(int[] number)
    {
        Console.Clear();
        for (int i = 0; i < number.Length; i++)
        {
            Array.Sort(number);
            Console.Write("Number " + (i + 1) + " = " + number[i] + "\n");
        }
    }
}

答案 1 :(得分:0)

你可以这样做

class Program
    {
        public static void Main(string[] args)
        {
            Program p = new Program();
            Random GenerateNumber = new Random();
            int[] number = new int[6];

            int[] generatedNumber = p.Generating(GenerateNumber, number);
            p.Ordering(generatedNumber);
        }

        public int[] Generating(Random GenerateNumber, int[] number)
        {
            Console.Clear();
            Console.WriteLine("Stage 1 : 6 random numbers have been 
            generated:\n");
            for (int c = 0; c < number.Length; c++)
            {
                if (number[c] == 0)
                {
                    number[c] = GenerateNumber.Next(1, 50);
                    Console.Write("Random number " + (c + 1) + " = " + number[c] 
                    + "\n");
                }
            }
            return number;
        }

        public void Ordering(int[] number)
        {
            Console.Clear();
            for (int i = 0; i < number.Length; i++)
            {
                Array.Sort(number);
                Console.Write("Number " + (i + 1) + " = " + number[i] + "\n");
            }
        }
    }

https://dotnetfiddle.net/3K6P54

答案 2 :(得分:0)

如果你有更多的方法,比如使用你的数组,那么我建议你创建一个CLASS来封装你想要的行为。仅此一点:

public class MyNumberArrayWithMethods
{
    public int[] Numbers { get; set; }
    public Action<int, int> Writer { get; set; }

    //parametric constructor        
    public MyNumberArrayWithMethods(int length, Action<int, int> writer = null)
    {
        Numbers = new int[length];
        Writer = writer;
    }

    public void Generating(int lowerBound = 1, int upperBound = 50)
    {
        for (int c = 0; c < Number.Length; c++)
        {
            if (Number[c] == 0)
            {
                Number[c] = GenerateNumber.Next(lowerBound, upperBound);
                if(Writer != null)
                    Writer(c, Number[c])
            }
        }
    }

    public void Ordering()
    {
        Console.Clear();
        Array.Sort(Number);
        if(Writer != null)
            for (int i = 0; i < Number.Length; i++)
                Writer(i, Number[i]);
    }

    //... all other methods that work with "Number" array should go here
}

然后在你的主要

static void Main(string[] args)
{
    Action<int, int> writer = 
        (pos, num) => { Console.Write("Number " + (pos + 1) + " = " + num + "\n"); }
    var MyCtrl = new MyNumberArrayWithMethods(6, writer);
    MyCtrl.Generating();
    Ordering();
}

请注意我是如何在循环外移动Array.Sort调用的。如果你把它留在里面,你会将数组排序的数量与数组的长度一样多,我猜你不想要它。