I am trying to run the code below on .Net fiddle, and am encountering the following problem. The randomly generated target number seems to change and the hints (higher or lower) seem to change with your guesses. Why is this happening?
I'm also sure that the code could use some optimizations, I'm a beginner and using a chromebook at school to practice.
using System;
public class Program
{
public static void Main()
{
var random = new Random();
int i = random.Next(0, 101);
int tries = 5;
Console.WriteLine("I'm thinking of a number between 1 and 100. Guess it in 5 tries.");
Start:
string feedback = Console.ReadLine();
int guess = Convert.ToInt32(feedback);
if (guess == i)
{
Console.WriteLine("You won with " + tries + " tries left!");
}
else if (guess > i && tries > 1)
{
tries--;
Console.WriteLine("Try lower. " + tries + " tries left.");
goto Start;
}
else if (guess < i && tries > 1)
{
tries--;
Console.WriteLine("Try higher. " + tries + " tries left.");
goto Start;
}
else
{
Console.WriteLine("Sorry, you ran out of tries. The nubmer was " + i + ".");
}
}
}