none
How to store and add value to an int RRS feed

  • Question

  • Hello, I am trying to make something that would work like this...however i do not know how to store and change the value of wallet. Would appreciate the help!

                int wallet = 0;
                while (true)
                {
                    
                    Console.WriteLine("\ntype command:");
                    string answer = Console.ReadLine();
                    if (answer.Contains("dig"))
                    {
                        Console.WriteLine("You find 10 gold coins.", wallet + 10);
                        
                    }
                    else if (answer.Contains("wallet"))
                    {
                        Console.WriteLine("You have " + wallet + " gold coins."); 
                    }
                    
                    else
                    {
    
                    }
                }

    Friday, January 5, 2018 9:08 PM

Answers

  • you are actually just calculating the value on the fly and not storing it in your wallet variable, you need to assign it the calculated value so that you can have the previous value available in variable when next input comes throgh console.

    Change your code to:

    walllet = wallet + 10;  
    Console.WriteLine("You find 10 gold coins.", wallet);

    Your complete program would be like:

    int wallet = 0;
    
    while (true)
    {
                    
        Console.WriteLine("\ntype command:");
        string answer = Console.ReadLine();
    
        if (answer.Contains("dig"))
        {
           wallet = wallet + 10;
           Console.WriteLine("You find 10 gold coins.", wallet);
                        
        }
        else if (answer.Contains("wallet"))
        {
          Console.WriteLine("You have " + wallet + " gold coins."); 
        }           
        else
        {
    
        }
    }

    Hope it Helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    • Marked as answer by J3KB0T Friday, January 5, 2018 9:48 PM
    • Edited by Ehsan Sajjad Friday, January 5, 2018 9:50 PM
    Friday, January 5, 2018 9:18 PM