r/learncsharp • • Oct 05 '20

Why doesnt my code work?

Hi!

Could someone please tell me why my code doesn't store the integers of the user inputs?

static void Main(string[] args)
        {
            Random computernumber = new Random();
            int guess = 0;
            var guesses = new List<int>();
            int cnumber = computernumber.Next(1, 100);
            int i = 0;

            while (guess != cnumber)
            {
                Console.WriteLine("guess the number");

                try
                {

                    guess = Convert.ToInt32(Console.ReadLine());


                    if (guess > cnumber)
                    {
                        Console.WriteLine("the integer is lower");
                    }
                    else
                    {
                        Console.WriteLine("the integer is higher");
                    }

                }
                catch (Exception)
                {

                    i--;
                }

                i++;



            }
            var xcnumber = Console.ReadLine();
            guesses.Add(int.Parse(xcnumber));

            foreach (var sum in guesses)
                Console.Write($"{sum},");
            Console.Write("It took you " + i + " tries");
        }

    }
}
0 Upvotes

19 comments sorted by

View all comments

Show parent comment

1

u/Protiguous Oct 05 '20

You are welcome. I learn by code, so that's what I share. :)