【发布时间】:2018-01-25 19:27:07
【问题描述】:
我尝试将我制作的 Monopoly Banking python 编程转换为 C#。
我使用主模块是我声明所有全局变量的地方,但是,我不太明白如何在没有传递所有必需变量的情况下在另一个模块中调用模块。
例如,在第 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