sadpoints
sadpoints

Reputation: 11

While (true) problems, what did i do wrong?

Why when I type "gamble" the first time, only the if statement works? It's no use typing anything else, it still adds 10 woods. And why when I type anything else the first time, just the else statement works? It is no use typing "gamble, will continue saying " Write '' gamble '' to hit the tree. " PS: The variable its int = woods; and string gamble;

Console.WriteLine("You have {0} woods", woods);
Console.WriteLine("Write ''gamble'' to hit the tree");
gamble = Console.ReadLine();
bool loop = true;

while (loop)
{
    if (gamble.Contains("gamble"))
    {
        woods = woods + 10;
        Console.Clear();
    }
    else
    {
        Console.WriteLine("Write ''gamble'' to hit the tree");
    }

    Console.WriteLine("You have {0} woods", woods);
    Console.ReadLine();
}                      

Upvotes: 1

Views: 73

Answers (5)

weenie
weenie

Reputation: 11

The reason it's adding 10 wood regardless if there is something else than "gamble" in the console line, is because you're writing "gamble" in the returning message.

else {Console.WriteLine("Write ''gamble'' to hit the tree");} is the problem here.

You can fix it by either not writing the word "gamble" inside the returning message, or find a clever way to not have it run in a while(true) loop.

You can, for example, use the main method to have it run the function you're going to run just once.

Something like this.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        // Set a `wood` variable for the class.
        protected int wood { get; set; }

        static void Main(string[] args)
        {
            Program program = new Program(); // Making use of non-static methods.
            program.Handler();
        }

        public void Handler()
        {
            Console.WriteLine("Write \"gamble\" to hit the tree.");
            string message = Console.ReadLine();

            if (message == "gamble")
            {
                addWood(); // Call the non-static method.
            }


        }

        public bool addWood()
        {
            this.wood = this.wood + 10;

            Console.WriteLine("You now have {0} wood!", this.wood);

            Handler(); // Call the Handler() method again for infinite loop.


            return true;
        }
    }
}

WARNING: The program will exit if there is something else than "gamble" written.

Upvotes: 0

Cihan Barış
Cihan Barış

Reputation: 26

Because loop is always true. You should change it to false after if and else statements...

Upvotes: 0

user10164217
user10164217

Reputation: 64

At the end of the while loop, you are doing Console.ReadLine() but not storing it. You need gamble = Console.ReadLine() to store the scanned string in the "gamble" variable.

Upvotes: 0

Niels van Reijmersdal
Niels van Reijmersdal

Reputation: 2045

    gamble = Console.ReadLine();

You only set gamble in the beginning. In the loop it is never changed. So it keeps using the first value over and over again.

Add gamble = to the last line of the loop.

Upvotes: 1

TheGeneral
TheGeneral

Reputation: 81523

If I understand what your are describing, you forgot to read into gamble again

   Console.WriteLine("You have {0} woods", woods);
   gamble = Console.ReadLine();
}    

Upvotes: 1

Related Questions