简体   繁体   中英

Returning int from one method to use in another method

Thanks for all the quick responses. They all really helped.

Hi all I'm new to C# and strongly typed languages.

I'm trying to return the int amount from my WithdrawAmount method so that I can then use it as a parameter in my DispenseCash cash method. I am getting the error "The name 'amount' does not exist in the current context".

What am I doing wrong and if it's not too much trouble could I be directed to online resources to learn more on the problem. Thanks :).

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

You are getting an error because you haven't declared a variable called amount .

I believe you wanted to declare a variable amount and assign the return value of the call to WithdrawAmount to it:

int amount = WithdrawAmount(whichAccount);

Then use it to call DispenseCash :

DispenseCash(amount, whichAccount, invalidAmount);

Note you can also do this inline:

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

Furthermore, looking at your code, you will most likely be in an infinite loop since nothing changes the value of invalidAmount . I believe instead of passing it as a parameter in DispenseCash and comparing the existing value, you really wanted to generate the value from DispenseCash and return it. Therefore having

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

Note I have inverted the logic since it makes more sense to return true from a successful dispensement of cash, rather than to return whether it was invalid or not.

your withdraw amount method is fine.

Though this line would be giving you error

DispenseCash(amount, whichAccount, invalidAmount);

because you haven't declared amount anywhere in your code.

as you want to return amount from withdrawamount method and then call dispense cash method. So, what you basically need is this.

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

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

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

It's quite simple actually:

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

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

See, when you have a return value, you have to store it somewhere if you wish for it to be recognized by other functions (and just by the rest of the program, really). As for good resources, can't go wrong with following these .

try this

do
{
    int amount = WithdrawAmount(whichAccount);

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

You have to assign the return value of the call to WithdrawAmount to a variable:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM