我遇到了將十進制變量傳遞給函數的問題。我想要做的是:在代碼的頂部,我聲明變量Money
爲十進制private decimal Money
,然後將該值設置爲0
。我想在AddMoney
函數中使用該變量,但是當我聲明函數static void AddMoney
時,我不知道要在()
中使用在函數外聲明的變量的參數。如何傳遞參數
namespace program
{
class data
{
private decimal Money = 0;
}
class Program
{
static void Main(string[] args)
{
Menu();
}
static void Menu()
{
ConsoleKeyInfo cki;
switch (cki.Key)
{
case ConsoleKey.NumPad1:
case ConsoleKey.D1:
Home();
break;
case ConsoleKey.NumPad2:
case ConsoleKey.D2:
AddMoney();
break;
static void Home()
{
Menu();
}
static void AddMoney()
{
Clear();
ConsoleKeyInfo cki;
string imputMoney;
decimal tempMoney;
PrintToScreen(1, 1, "" + Money);
PrintToScreen(1, 2, "Please select an option from bellow: ");
PrintToScreen(1, 3, "1. Update the current amount (add money to existing amount)");
PrintToScreen(1, 4, "2. Update a new total (rase and change the total)");
cki = Console.ReadKey();
switch (cki.Key)
{
case ConsoleKey.NumPad1:
case ConsoleKey.D1:
Clear();
PrintToScreen(1, 1, "Please enter a amount: ");
imputMoney = Console.ReadLine();
tempMoney = System.Convert.ToDecimal(imputMoney);
tempMoney + Money = Money;
PrintToScreen(1, 1, "" + Money);
Console.ReadKey();
Menu();
break;
case ConsoleKey.NumPad2:
case ConsoleKey.D2:
Clear();
PrintToScreen(1, 1, "Please enter a amount: ");
imputMoney = Console.ReadLine();
tempMoney = System.Convert.ToDecimal(imputMoney);
Money = 0;
tempMoney = Money;
PrintToScreen(1, 1, "" + Money);
Console.ReadKey();
Menu();
break;
}