2013-08-30 145 views
0

感謝您的所有快速響應。他們都很有幫助。從一種方法返回int以在另一種方法中使用

大家好我是C#和強類型語言的新手。

我想從我的WithdrawAmount方法返回int數量,以便我可以在DispenseCash現金方法中使用它作爲參數。我收到錯誤「名稱數量」在當前上下文中不存在「。

我在做什麼錯,如果沒有太多的麻煩,我可以指示在線資源瞭解更多的問題。謝謝 :)。

int whichAccount = int.Parse(Console.ReadLine()); 
do 
    { 
     WithdrawAmount(whichAccount); 

     DispenseCash(amount, whichAccount, invalidAmount); 
    } while (invalidAmount == true); 

// end of little example segment of Main 



static int WithdrawAmount(int whichAccount) 
    { 
     Console.Write("\nPlease enter how much you would like to withdraw: $"); 
     int amount = int.Parse(Console.ReadLine()); 
     return amount; 
    }//end WithdrawAmount 



private static bool DispenseCash(int amount, int whichAccount, bool invalidAmount) 
     { 
      int numOf20s; 
      int numOf50s; 

      if (amount % 20 == 0) 
      { 
       numOf20s = amount/20; 
       Console.WriteLine("Number of 20's = {0}", numOf20s); 
       accountBalances[whichAccount] = (accountBalances[whichAccount]) - amount; 
       return invalidAmount == false; 


      } 
      else if (amount % 50 == 0) 
      { 
       numOf50s = amount/50; 
       Console.WriteLine("Number of 50's = {0}", numOf50s); 
       return invalidAmount == false; 
      } 


      else if ((amount - 50) % 20 == 0) 
      { 
       numOf50s = 1; 
       numOf20s = (amount - 50)/20; 
       Console.WriteLine("Number of 20's = {0}", numOf20s); 
       Console.WriteLine("Number of 50's = {0}", numOf50s); 
       return invalidAmount == false; 
      } 

      else 
      { 
       Console.WriteLine("Invalid entry"); 
       return invalidAmount == true; 
      } 

     }//end DispenseCash 
+0

有什麼用這條線INT whichAccount = int.Parse的(到Console.ReadLine()); – Kalyan

+0

這樣用戶可以選擇他們想要退出的銀行賬戶。 – user2281248

+0

好的。您正在將whichAccount傳遞給WithdrawAmount()方法。但是你沒有使用該方法中的哪個帳戶。 – Kalyan

回答

1

由於您尚未聲明名爲amount的變量,您將收到錯誤消息。

我相信你想聲明一個變量amount並分配調用的返回值WithdrawAmount它:

int amount = WithdrawAmount(whichAccount); 

然後用它打電話DispenseCash

DispenseCash(amount, whichAccount, invalidAmount); 

請注意,您也可以直接執行此操作:

DispenseCash(WithdrawAmount(whichAccount), whichAccount, invalidAmount); 

此外,看你的代碼,你很可能會處於無限循環,因爲沒有任何東西會改變invalidAmount的值。我相信不是將它作爲參數傳入DispenseCash並比較現有值,而是真的想從DispenseCash生成值並將其返回。因此有

invalidAmount = !DispenseCash(WithdrawAmount(whichAccount), whichAccount); 

注意我有相反的邏輯,因爲它更有意義,從現金成功dispensement返回true,而不是回到它是否是無效或不。

+0

謝謝,你認爲我應該在哪裏分配它? – user2281248

+0

@ user2281248你在哪裏調用它(在main?中)。也許你應該做一個關於「變量範圍」的谷歌搜索。你可能會認爲你已經有了一個名爲「amount」的變量,但這只是在WithdrawAmount方法中。 –

+0

int amount = WithdrawAmount(whichAccount);作品。謝謝 – user2281248

0

您的提款方式沒問題。

雖然這條線會爲您提供錯誤

DispenseCash(amount, whichAccount, invalidAmount); 

因爲你還沒有宣佈amount在你的代碼的任何地方。

您想從withdrawamount方法中返回金額,然後調用點鈔現金方法。所以,你基本需要的是這個。

int amount =WithdrawAmount(whichAccount); 
DispenseCash(amount, whichAccount, invalidAmount); 
0

因爲你WithdrawAmount(whichAccount)回報INT沒有必要分配一個新的變量和設定值給它,而不是你可以試試這個

DispenseCash(WithdrawAmount(whichAccount), whichAccount, invalidAmount); 
0

這是很簡單的實際上是:

int whichAccount = int.Parse(Console.ReadLine()); 
do 
    { 
     int amount = WithdrawAmount(whichAccount); 

     DispenseCash(amount, whichAccount, invalidAmount); 
    } while (invalidAmount == true); 

看到,當你有一個返回值時,如果你希望它被其他函數所識別,那麼你必須將它存儲在某個地方(並且真的是這個程序的其餘部分)。至於良好的資源,跟着these不能出錯。

+0

謝謝你,我檢查一下 – user2281248

0

試試這個

do 
{ 
    int amount = WithdrawAmount(whichAccount); 

    DispenseCash(amount, whichAccount, invalidAmount); 
} while (invalidAmount == true); 
0

你要調用的返回值賦給WithdrawAmount給一個變量:

int whichAccount = int.Parse(Console.ReadLine()); 
do 
{ 
    int amount=WithdrawAmount(whichAccount); 

    DispenseCash(amount, whichAccount, invalidAmount); 
} while (invalidAmount == true); 
相關問題