从一个方法返回int以在另一个方法中使用

时间:2013-08-30 05:21:14

标签: c# methods return

感谢所有快速回复。他们都非常有帮助。

大家好我是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

6 个答案:

答案 0 :(得分: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);

注意我已经颠倒了逻辑,因为从成功分配现金返回true更有意义,而不是返回它是否无效。

答案 1 :(得分:0)

您的提款金额方法没问题。

虽然这行会给你错误

DispenseCash(amount, whichAccount, invalidAmount);

因为您尚未在代码中的任何位置声明amount

因为您希望从withdrawamount方法返回金额,然后调用分配现金方法。所以,你基本上需要的就是这个。

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

答案 2 :(得分:0)

由于您的WithdrawAmount(whichAccount)返回 int ,因此无需分配新变量并为其设置值,而是可以尝试此操作

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

答案 3 :(得分:0)

实际上很简单:

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

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

请注意,当您有返回值时,如果您希望将其存储在某个地方,则必须将其存储在某个地方(实际上只能由程序的其余部分识别)。至于良好的资源,跟随these不会出错。

答案 4 :(得分:0)

试试这个

do
{
    int amount = WithdrawAmount(whichAccount);

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

答案 5 :(得分:0)

您必须将对WithdrawAmount的调用的返回值赋给变量:

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

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