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

本文关键字:方法 另一个 int 返回 一个 | 更新日期: 2023-09-27 18:29:32

感谢您的快速回复。他们真的帮了忙。

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

我正试图从我的提款金额方法返回int金额,这样我就可以在DispenseCash现金方法中使用它作为参数。我收到错误"名称'amount'在当前上下文中不存在"。

我做错了什么?如果不是太麻烦的话,我可以通过在线资源了解更多关于这个问题的信息。谢谢:)。

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以在另一个方法中使用

由于您没有声明一个名为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比返回它是否无效更有意义。

您的提款金额方法很好。

虽然这条线会给你错误

DispenseCash(amount, whichAccount, invalidAmount);

因为您在代码中的任何位置都没有声明amount

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

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

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

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

实际上很简单:

int whichAccount = int.Parse(Console.ReadLine());
do
    {
         int amount = WithdrawAmount(whichAccount);
         DispenseCash(amount, whichAccount, invalidAmount);
    } while (invalidAmount == true);

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

尝试这个

do
{
    int amount = WithdrawAmount(whichAccount);
     DispenseCash(amount, whichAccount, invalidAmount);
} while (invalidAmount == true);

您必须将调用提款金额的返回值分配给一个变量:

int whichAccount = int.Parse(Console.ReadLine());
do
{
     int amount=WithdrawAmount(whichAccount);
     DispenseCash(amount, whichAccount, invalidAmount);
} while (invalidAmount == true);