【问题标题】:Returning int from one method to use in another method从一种方法返回 int 以在另一种方法中使用
【发布时间】:2013-08-30 05:21:14
【问题描述】:

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

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

【问题讨论】:

  • 这行有什么用 int whichAccount = int.Parse(Console.ReadLine());
  • 这样用户就可以选择他们希望从哪个银行账户提款。
  • ok.. 您正在将 whichAccount 传递给 WithdrawAmount() 方法。但是您没有在该方法中使用 whichAccount。

标签: c# methods return


【解决方案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 比返回是否无效更有意义。

【讨论】:

  • 谢谢,你认为我应该把它分配到哪里?
  • @user2281248 你在哪里调用它(主要?)。也许您应该在 Google 上搜索“可变范围”。您可能认为您已经有一个名为“amount”的变量,但它只在 WithdrawAmount 方法中。
【解决方案2】:

你的取款方式没问题。

虽然这条线会给你错误

DispenseCash(amount, whichAccount, invalidAmount);

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

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      其实很简单:

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

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

      【讨论】:

        【解决方案5】:

        试试这个

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

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-03-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多