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!"); } } } } }
new Random().Next(1, 100) is poor for two reasons. (1) it's bad practice to
new` up multiple instances as it can lead to duplicate values. (2) If you want numbers from 1 to 100 you need to put.Next(1, 101)
. – Enigmativity