我无法弄清楚如何从另一种方法调用变量

时间:2016-10-10 03:42:28

标签: variables call

我正在尝试将另一个方法中的变量调用到我的数组中。 var Com =游戏的难度。但下面的方法是我尝试调用var Com,因为:var c = Com.GetChoice();

不知道为什么我无法弄清楚如何调用它。

public object SetDiff()
        {
            Console.WriteLine("Enter difficulty #: (1 = Easy, 2 = Normal, 3 = Impossible)");
            var diff = Console.ReadLine();
            int mode;
            int.TryParse(diff, out mode);

            if (mode == 1)
            {
                Console.Clear();
                var Com = new Easy();
                return Com;
            }
            if (mode == 2)
            {
                Console.Clear();
                var Com = new Medium();
                return Com;
            }
            if (mode == 3)
            {
                Console.Clear();
                var Com = new Hard();
                return Com;
            }
            else
            {
                Console.WriteLine("That is not a valid input.");
                return SetDiff();
            }
        } // Apparently you can't set variables in a switch.

        public int[] FaceOff(int num)
        {
            int PlayerWin = 0;
            int ComWin = 0;
            int Tie = num + 1;
            // TODO : Get rid of TIES! 
            for (int i = 0; i < num; i++)
            {
                var p = p1.GetChoice();
                var c = Com.GetChoice();

1 个答案:

答案 0 :(得分:0)

您有许多不同的选择:

传递参数

 public int[] FaceOff(int num, int Com){...}

制作一个&#34;全球&#34;变量

private int Com;

我还建议你学习OOP(面向对象编程)的基础知识。