如何在离开方法时保存变量值



我有一个方法可以计算用户从ATM取款的次数(因为有限制(,还可以计算用户当天提取的金额。但是,count var 和 amountWithdrawn 变量中的值在离开该方法时都会丢失,如何保持它们的"保存"?
另外作为旁注,我有一个名为Account的类,它具有平衡等,最好将它们放在那里吗?但也想知道是否可以将变量保存在方法中以备将来参考。

public decimal WithDraw()
{
    int timesWithdrawn = 9;
    decimal amountWithdrawnToday = 0;
    decimal money = 0;
    bool success = false;
    if (timesWithdrawn < 10)
    {
        do
        {
            //Console.WriteLine("{0} available to withdraw.", FundsAvailable);
            Console.WriteLine("How much would you like to withdraw?");
            try
            {
                money = decimal.Parse(Console.ReadLine());
                if (money % 5 == 0 && money <= account.CurrentBalance && money <= 1000)
                {
                    success = true;
                }
                if (money == 0)
                {
                    bool exit = true;
                    Console.WriteLine("Do you want to exit? Type "yes", or "no".");
                    while (exit == true)
                    {
                        string response = Console.ReadLine();
                        if (response.ToLower() == "yes")
                        {
                            break;
                        }
                        else
                        {
                            exit = false;
                        }
                    }
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("Please enter a number to withdraw.");
            }
        } while (success == false);
        //do while this is true
        Console.WriteLine(account.CurrentBalance);
        Console.WriteLine("Withdrawing {0} pounds.", money);
        Console.WriteLine("You have {0} remaining in your account.", account.CurrentBalance - money);
        amountWithdrawnToday += money;
        timesWithdrawn += 1;
        Console.WriteLine("{0} pounds withdrawn today", amountWithdrawnToday);
        return account.CurrentBalance -= money;
    }
    else
    {
        Console.WriteLine("You have exceeded daily withdrawls. You have withdrawn {0}", amountWithdrawnToday);
        return amountWithdrawnToday;
    }
}

我建议你需要将这些变量放在Account类中,我也建议你可以把withdraw方法本身放在Account类中,这可能对OOP更友好。

要保存timesWithdrawn号,您只需将其设置为类实例即可,而不是使其成为局部变量

这是代码

class Account
{
    public decimal CurrentBalance { get; set; }
    public int timesWithdrawn { get; set; } = 9;
    public decimal WithDraw()
    {
        decimal amountWithdrawnToday = 0;
        decimal money = 0;
        bool success = false;
        if (timesWithdrawn < 10)
        {
            do
            {
                //Console.WriteLine("{0} available to withdraw.", FundsAvailable);
                Console.WriteLine("How much would you like to withdraw?");
                try
                {
                    money = decimal.Parse(Console.ReadLine());
                    if (money % 5 == 0 && money <= CurrentBalance && money <= 1000)
                    {
                        success = true;
                    }
                    if (money == 0)
                    {
                        bool exit = true;
                        Console.WriteLine("Do you want to exit? Type "yes", or "no".");
                        while (exit == true)
                        {
                            string response = Console.ReadLine();
                            if (response.ToLower() == "yes")
                            {
                                break;
                            }
                            else
                            {
                                exit = false;
                            }
                        }
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("Please enter a number to withdraw.");
                }
            } while (success == false);
            //do while this is true
            Console.WriteLine(CurrentBalance);
            Console.WriteLine("Withdrawing {0} pounds.", money);
            Console.WriteLine("You have {0} remaining in your account.", CurrentBalance - money);
            amountWithdrawnToday += money;
            timesWithdrawn += 1;
            Console.WriteLine("{0} pounds withdrawn today", amountWithdrawnToday);
            return CurrentBalance -= money;
        }
        else
        {
            Console.WriteLine("You have exceeded daily withdrawls. You have withdrawn {0}", amountWithdrawnToday);
            return amountWithdrawnToday;
        }
    }
}

正如您从代码中注意到的那样,我删除了对 account 变量的引用,并将CurrentBalance作为实例变量timesWithdrawn .

即使在方法完成后,这也可以保留timesWithdrawn的值。

只要程序正在运行,它就会保存在那里,但是一旦完全重置,所以是的,您必须将其保存在数据库中的某个位置,Excel表格或文本文档中在 C# 中将数据保存到文件

如果您需要一些帮助,请查看如何做这样的事情

您可以将"out"参数传递给函数,它基本上是您发送给函数的变量,该变量将其值保存在函数之外。例如:

public void WithDraw(out int c) {
    c = something ; //c must receive a value during the call
}  
int myvar; // the parameter which will hold the value when the function returns
WithDraw(out myvar);  //call to the function
myvar  //will now hold a value from the function (something)

您还可以考虑返回元组,或将要保存的值放入"全局变量"中。

最新更新