【问题标题】:C# Passing ParametersC# 传递参数
【发布时间】:2018-01-25 19:27:07
【问题描述】:

我尝试将我制作的 Monopoly Banking python 编程转换为 C#。

我使用主模块是我声明所有全局变量的地方,但是,我不太明白如何在没有传递所有必需变量的情况下在另一个模块中调用模块。

https://pastebin.com/bVdt5nrP

例如,在第 50 行,我尝试运行 SetCustom 模块,但是我没有将所需的所有参数传递到 MainMenu,我真的不想将每个参数传递到每个模块,因为这只会以混乱和混乱结束。有没有我可以解决这个问题,还是我必须把所有事情都搞得一团糟?

提前致谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonopolyCSharpNonForm
{
    class Program
    {
        static void Main(string[] args)
        {

            //Primary Settings
            int StartingBallance = 2000;
            int PassGo = 200;
            int Bail = 50;
            int IncomeTax = 100;
            int SuperTax = 200;
            string TaxLocation = 1; // 1 goes to the bank

            //Players/Economy
            string Player1 = "Player1";
            string Player2 = "";
            string Player3 = "";
            string Player4 = "";
            int Ballance1 = 2000;
            int Ballance2 = 2000;
            int Ballance3 = 2000;
            int Ballance4 = 2000;
            int FreeParking = 0;
            string[] BankRupties = { };

            MainMenu(); // Calling Main Menu
        }

        static void MainMenu()
        {
            Console.WriteLine("Welcome to Monpoly Banker V4");
            Console.WriteLine("Would you to use the Default or Custom settings?"); //Ask for Default or Custom option
            string LocalOption = Console.ReadLine(); //Record the option as a local variable 'LocalOption'
            while (LocalOption != "Default" && LocalOption != "Custom")//Check if LocalOption is 'Default' or 'Custom'
            {
                Console.WriteLine("Niether option has been specified, type 'Default' or 'Custom'"); // If not state there is an Error.
                LocalOption = Console.ReadLine(); //Record the input again.
            }

            if (LocalOption == "Custom") // Check if local variable is equal to custom.
            {
                SetCustom(ref StartingBallance, ref PassGo, ref Bail, ref IncomeTax, ref SuperTax, ref TaxLocation); //If it is run the SetCustom module below with the required parameters
            }
            else //Anything else it runs GettingNames module with required parameters
            {
                GettingNames(ref Player1, ref Player2, ref Player3, ref Player4);
            }
        }

        static void SetCustom(ref int StartingBallance, ref int PassGo, ref int Bail, ref int IncomeTax, ref int SuperTax, ref string TaxLocation)
        {
            Console.WriteLine("\nHow much money is everyone starting with? "); // Ask for Starting Ballance
            StartingBallance = Convert.ToInt32(Console.ReadLine()); // Change the StartingBallance to entered integer
            Console.WriteLine("\nHow much money if you pass go? "); // Ask how much you get for passing go
            PassGo = Convert.ToInt32(Console.ReadLine()); // change PassGo to entered integer
            Console.WriteLine("\nHow much will bail cost? "); // Ash how much Bail is going to cost
            Bail = Convert.ToInt32(Console.ReadLine()); // change Bail to entered integer
            Console.WriteLine("\nHow much will income tax cost? "); // Ask how much Income Tax will cost
            IncomeTax = Convert.ToInt32(Console.ReadLine()); // Change IncomeTax to entered Integer
            Console.WriteLine("\nHow much will super tax cost? "); // Ask how much Super Tax will cost
            SuperTax = Convert.ToInt32(Console.ReadLine()); // Record the entered integer
            Console.WriteLine("\nDoes tax go to the bank for free parking? (1 = Bank | 2 = Free Parking)"); // Ask for where free parking will go (1/2 to make options easier)
            TaxLocation = Console.ReadLine(); // Change TaxLocation to entered String
            while (TaxLocation != "1" && TaxLocation != "2") // Check TaxLocation is either '1' or '2'
            {
                Console.WriteLine("Invalid Input. Try again."); // If not state its invalid input
                TaxLocation = Console.ReadLine(); // Record entered Integer
            }

            GettingNames(ref Player1, ref Player2, ref Player3, ref Player4); // When done continue on to module Getting Names passing required parameters

        }

        static void GettingNames(ref string Player1, ref string Player2, ref string Player3, ref string Player4)
        {
            Console.WriteLine("\nWhat is Player1 name? "); // Ask Player1 name
            Player1 = Console.ReadLine(); // Set Player1 to entered string
            Console.WriteLine("\nWhat is Player2 name? "); // Ask Player2 name
            Player2 = Console.ReadLine(); // Set Player1 to entered string
            Console.WriteLine("\nWhat is Player3 name? "); // Ask Player3 name
            Player3 = Console.ReadLine(); // Set Player1 to entered string
            Console.WriteLine("\nWhat is Player4 name? "); // Ask Player4 name
            Player4 = Console.ReadLine(); // Set Player1 to entered string

            Console.WriteLine("All 4 player names have been entered."); // State all names entered and received
        }
    }
}

【问题讨论】:

  • 创建一个具有所需属性的 Player 类,然后您可以根据需要传递对象,而不是传递单个变量
  • 我建议您在编写任何代码之前阅读一些有关 C# 的文档或教程。在此示例中,您可以使用 oop、模式、全局类变量、数组... 显着简化代码

标签: c# variables module parameter-passing


【解决方案1】:

就个人而言,我想我会先尝试将其分解为单独的类。例如,您可以创建一个名为 Game 的类,其中包含游戏的设置。这些设置可以通过构造函数设置为默认设置,您可以提供自定义构造函数来设置个别事物。类似于以下内容:

public class Game
{
    public int PassGo;
    public int Bail;

    public ClassName()
    {
        PassGo = 200;
        Bail = 50;
    }

    public ClassName(int customPassGo, int customBail)
    {
        PassGo = customPassGo;
        Bail = customBail;
    }
}

如果您愿意,还可以通过以下方式为这些参数设置默认值:

public ClassName(int customPassGo = 200, int customBail = 50)
{
    PassGo = customPassGo;
    Bail = customBail;
}

您也可以从播放器中创建对象。使用对象可以大大简化您在此处尝试执行的操作。希望这会有所帮助!

【讨论】:

  • 好的,我已经按照你的建议做了,当我调用主菜单时遇到了另一个问题:错误 CS0120 An object reference is required for the non-static field, method, or属性 'Program.MainMenu() pastebin.com/MDHNgfpg
  • 正确,Main 方法是一个静态方法,因此 MainMenu 也需要是一个静态方法,或者您可以用它创建一个类并以这种方式使用它。在这种情况下,我可能会将其设为一个名为 BuildMainMenu 的静态方法,然后将您的代码放入其中。您可以在此处阅读有关静态含义的更多信息:stackoverflow.com/questions/9410688/static-keyword-in-c-sharp
猜你喜欢
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多