离开方法时如何保存变量值?
Posted
技术标签:
【中文标题】离开方法时如何保存变量值?【英文标题】:How to save a variables value when leaving the method? 【发布时间】:2017-05-27 09:52:43 【问题描述】:我有一个方法可以计算用户从 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;
【问题讨论】:
在我看来,对于这种特定用途,最好使用帐户类,因为它包含有关帐户的信息。此外,可以创建一个“包装器”类,从包含方法外所需的所有信息的方法返回。另一种选择是使用 out 或 ref 参数。发送参数会在发送它们的上下文中更改它们。你可以在这里阅读更多关于它的信息link 【参考方案1】:我建议你需要将这些变量放在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
的值。
【讨论】:
【参考方案2】:只要程序正在运行,它就会保存在那里,但是一旦它完全重置,所以是的,你必须将它保存在数据库中的某个地方,一个 Excel 表格或一个文本文档中 Saving data to a file in C#
如果你需要一些帮助来做这样的事情,看看这个
【讨论】:
虽然你是对的,但操作员没有问这个问题,操作员在完成激发它后询问从方法中获取值【参考方案3】:您可以将“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)
您还可以考虑返回一个元组,或者将您希望保存的值放入“全局变量”中。
【讨论】:
以上是关于离开方法时如何保存变量值?的主要内容,如果未能解决你的问题,请参考以下文章
保存MATLAB中间变量值的方法:保存为txt文件或者mat文件