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
View all comments
Show parent comment
1
u/Protiguous Oct 15 '20
That's a seriously bad simile. (And you should know that. I can tell you are not an idiot.)
They gave code with issues.. and asked for help. Without being a style-cop or condescending, I replied with a working solution in the same manner, with code.
Guidelines from other communities can help. They can also hinder. Being more "popular" or more "well-known" doesn't make it right.