2017-05-27 46 views
0

我有計數的時間用戶已經從ATM提取的金額(作爲是有一定限度),並且還向上計數的錢用戶在一天已經取出的量的方法。然而,計數變量和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; 
    } 
} 
+0

在我看來,對於這個特定的使用,最好能夠使用的帳戶類,因爲它保存有關的帳戶信息。此外,可以創建一個「包裝器」類,從包含方法外所需信息的方法返回。另一種選擇是使用out或ref參數。發送參數會在發送它們的上下文中更改它們。你可以閱讀更多關於它這裏[鏈接](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/method-parameters) –

回答

1

我建議你需要把這些變量放在Account類中,同時我建議你可以把withdraw方法本身放在Account類中,這樣可以更友好。

,並保存timesWithdrawn數,你只需要讓它作爲類的實例vairable的而不是把它局部變量

這裏是代碼

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的價值。

0

只要在程序運行它被保存在那裏,但一旦相當它重置,所以是的,你必須從某個地方將它保存在數據庫中的Excel表格或文本文件 Saving data to a file in C#

檢查了這一點,如果你需要一些幫助,如何做到這樣的東西

+0

,而你是對的,運算不是問這一點,OP在完成激勵後詢問從方法中獲得價值 –

0

你可以傳遞一個「出」參數的功能,它基本上你發送的功能,保持其在函數外賦值的變量。 例如:

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) 

你也可以考慮返回一個元組,或將要保存成一個「全局變量」的值。

相關問題