将一个整数导入另一个 class

Import a int to a another class

我有 2 个不同的 classes。

1 class 从哪里开始并选择 class:

class Program
{
    static void Main(string[] args)
    {
        Boolean Choose = false;
        string Input;
        int Tactic = 0, Defend = 0, Attack = 0;
        MenuTacticus HubTacticus = new MenuTacticus();

        Console.WriteLine("Welcome to the PanaKnockOut Alpha V.");
        Console.WriteLine("There are three classes you can choose: Tacticus, Attacker, Defender \nEnter the name of the class to choose the class. \nFor more info about the classes enter ?");
        while (Choose == false){
            Console.WriteLine("What do you choose?");
            Console.WriteLine("");
            Input = (Console.ReadLine());
            Input.ToLower();
            switch (Input)
            {
                case "tacticus":
                    Choose = true;
                    Tactic = 60;
                    Defend = 30;
                    Attack = 30;
                    HubTacticus.HubTacticus();
                    break;
                case "attacker":
                    Tactic = 30;
                    Defend = 30;
                    Attack = 60;
                    Choose = true;
                    break;
                case "defender":
                    Tactic = 30;
                    Defend = 60;
                    Attack = 30;
                    Choose = true;
                    break;
                default:
                    Console.WriteLine("This isn't a class at this moment.");
                    Console.WriteLine("Try Again!");
                    break;
            } 
        }

    }
}

还有一个 class 当你选择 class 时你会得到:

class MenuTacticus
{
    public void HubTacticus()
    {
        string Choice;
        Boolean HUB = true;
        ArenaTacticus ARENAT = new ArenaTacticus();
        //tactic from other class here 
        //defend from other class here
        //attack from other class here

        while (HUB == true)
        {
            Console.WriteLine("Welcome to the Main Menu you wil come here more \n(I know it looks boring but I am a starting programmer so i can't build a interface/menu");
            Console.WriteLine("What do you want to do now? \nPlay a match(type: play)/Train(type: train");
            Choice = Console.ReadLine();
            Choice.ToLower();
            switch (Choice)
            {
                case "play":
                    ARENAT.ARENAT();
                    break;
                case "train":
                    break;
            }
        }
    }
}

但我有一个问题,我不知道如何从 Programm[=26= 导入 int: Tactic/Defend/Attack ] class 到 MenuTacticus class.

向您的 HubTacticus() 函数添加参数:

public void HubTacticus(int tactic, int defend, int attack)

在我看来,您应该使用一个对象并将其传递给方法。对象的好处是可重用性和向这些属性附加更多功能的能力,如果需要,稍后在您的代码中(例如计算或派生属性):

public class Stats
{
   public Stats(int tactic, int defend, int attack)
   {
      Tactic = tactic;
      Defend = defend;
      Attack = attack;
   }
   public int Tactic {get;private set;}
   public int Defend {get;private set;}
   public int Attack {get;private set;}
}

然后向您的 HubTacticus 方法添加一个参数:

class MenuTacticus
{
    public void HubTacticus(Stats stats)
    {
        string Choice;
        Boolean HUB = true;
        ArenaTacticus ARENAT = new ArenaTacticus();
        //stats.Tactics
        //stats.Defend
        //stats.Attack

        while (HUB == true)
        {
            ...
        }
    }
}

然后调整你的主逻辑:

class Program
{
    static void Main(string[] args)
    {
        Boolean Choose = false;
        string Input;
        Stats stats;
        MenuTacticus HubTacticus = new MenuTacticus();

        Console.WriteLine("Welcome to the PanaKnockOut Alpha V.");
        Console.WriteLine("There are three classes you can choose: Tacticus, Attacker, Defender \nEnter the name of the class to choose the class. \nFor more info about the classes enter ?");
        while (Choose == false){
            Console.WriteLine("What do you choose?");
            Console.WriteLine("");
            Input = (Console.ReadLine());
            Input.ToLower();
            switch (Input)
            {
                case "tacticus":
                    Choose = true;
                    //Tactic = 60;
                    //Defend = 30;
                    //Attack = 30;
                    stats = new Stats(60,30,30);
                    HubTacticus.HubTacticus(stats);
                    break;
                case "attacker":
                    //Tactic = 30;
                    //Defend = 30;
                    //Attack = 60;
                    stats = new Stats(30,30,60);
                    Choose = true;
                    break;
                case "defender":
                    //Tactic = 30;
                    //Defend = 60;
                    //Attack = 30;
                    stats = new Stats(30,60,30);
                    Choose = true;
                    break;
                default:
                    Console.WriteLine("This isn't a class at this moment.");
                    Console.WriteLine("Try Again!");
                    break;
            } 
        }

    }
}