mercredi 24 juin 2020

console application where the user has 5 tries to guess number between 1 and 100

I have created a console application where the user has 5 tries to guess number between 1 and 100. After 5 guesses the game ends, but I don’t know how to introduce at the 5th wrong intent something like “you have achieved maximum of guesses! The answer was number (X). I have tried different ways ,but is not working. This is my program

using System;

namespace Guessing_Game_4 { class Program { static void Main(string[] args) {

        var number = new Random().Next(1, 100);
        Console.WriteLine("Try and guess any number between 1-100. You have 5 guesses Max!");

        for (var i = 0; i < 5; i++)
        {

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

           
            if (guess == number)
            {
                Console.WriteLine("You got it!");
                break;
            }
            else
            {
                Console.WriteLine(guess + " is not correct! Try again!");

            }
            
    }   }
}

}




Aucun commentaire:

Enregistrer un commentaire