感謝您的所有快速響應。他們都很有幫助。從一種方法返回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
有什麼用這條線INT whichAccount = int.Parse的(到Console.ReadLine()); – Kalyan
這樣用戶可以選擇他們想要退出的銀行賬戶。 – user2281248
好的。您正在將whichAccount傳遞給WithdrawAmount()方法。但是你沒有使用該方法中的哪個帳戶。 – Kalyan