0

感谢所有快速回复。他们都真的有帮助。

大家好,我是 C# 和强类型语言的新手。

我正在尝试从我的 WithdrawAmount 方法返回 int 金额,以便我可以将其用作 DispenseCash cash 方法中的参数。我收到错误“当前上下文中不存在名称‘金额’”。

我做错了什么,如果不是太麻烦,我可以被引导到在线资源以了解有关该问题的更多信息。谢谢 :)。

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
4

6 回答 6

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,而不是返回它是否无效。

于 2013-08-30T05:23:49.860 回答
0

您的提款金额方法很好。

虽然这条线会给你错误

DispenseCash(amount, whichAccount, invalidAmount);

因为您没有amount在代码中的任何地方声明。

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

int amount =WithdrawAmount(whichAccount);
DispenseCash(amount, whichAccount, invalidAmount);
于 2013-08-30T05:23:38.493 回答
0

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

 DispenseCash(WithdrawAmount(whichAccount), whichAccount, invalidAmount);
于 2013-08-30T05:25:31.827 回答
0

其实很简单:

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

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

看,当你有一个返回值时,如果你希望它被其他函数识别(实际上只是被程序的其余部分识别),你必须将它存储在某个地方。至于好的资源,跟随这些不会出错。

于 2013-08-30T05:26:44.683 回答
0

试试这个

do
{
    int amount = WithdrawAmount(whichAccount);

     DispenseCash(amount, whichAccount, invalidAmount);
} while (invalidAmount == true);
于 2013-08-30T05:27:00.350 回答
0

您必须将调用 WithdrawAmount 的返回值分配给一个变量:

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

     DispenseCash(amount, whichAccount, invalidAmount);
} while (invalidAmount == true);
于 2013-08-30T05:30:55.723 回答