C# guessing game only one guess

Viewed 119

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!");
 }
 }
1 Answers

Edit: As you indeed only want one guess to be allowed, there is no loop needed and your code should work fine (apart form an if missing after the last else statement). To have a "game ended" notice at the end, just add

Console.WriteLine("Game ended.");

at the end of your code.

Also, there is one pair of curled brackets that is not needed in your code. The opening one after int rand_num = rd.Next(1, 10); and the closing one at the very end of the code.


And here remains the original answer:

At first, you want a loop around parts of your code to repeat as long as the answer was not correct or there are no more tries left (in case tries are limited):

const int tries = 10;              // Give the player 10 tries to guess correctly.
for (int i = 0; i < tries; i++)
{
    if (guessedCorrectly)
    {
        break;
    }
}

Second, you want to define the random number before starting the loop and you want to query new guesses from the player inside the loop:

//Set random number.
//Define Loop.
{
    //Get new guess from player.
    //Check if guess was correct.
    //Decide weather the loop must continue or ended.
}
//Display some result.

Third, you need to change the order of your if else statements. In your code, the statement inside the if (Math.Abs(guess - rand_num) <= 3) guard can never be executed, because you check for guess < rand_num and guess > rand_num before. And since you use else if, the following conditions will never be evaluated if the ones before were true. When using if else statements, alsways check for the more specific cases before checking for the more broad cases. But in your case you probably would want to just remove the else, so both conditions (and both texts) can be executed.

Related