i´m stuck with my guessing game where i should only have one chance to guess and then get a "the end" line. I have tried to make a break and breakLoop commands but i´t didn´t work and i have no idé how to fick it... Can someone please help me? here is my code, i know that it´s not optimal and it´s because i´m still learning C#.
Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 ocn 100, så
varsågod..\nskriv in ett tal");
var str = Console.ReadLine();
int guess = Convert.ToInt32(str);
//gonna make it random between 1-100
Random rd = new Random();
int rand_num = rd.Next(1, 10);
{
//when i guess right
if (guess == rand_num)
{
Console.WriteLine("Ditt Tal är rätt. grattis!");
}
//when it´s to small guess
else if (guess < rand_num)
{
Console.WriteLine("Ditt tal är för litet. gissa på ett större tal");
}
//when i guess to big
else if (guess > rand_num)
{
Console.WriteLine("Ditt tal är för stort. gissa på ett mindre tal");
}
// when i was close to the answer
else if (Math.Abs(guess - rand_num) <= 3)
{
Console.WriteLine("Du är dock nära och det bränns");
}
// when i guess a number thats over the number i chould guess
else (guess > 10)
{
Console.WriteLine("Du måste skriva in ett tal mellan 1 och 100!");
}
}