Member-only story

C# tutorial — Guess the number game

Alex
5 min readApr 22, 2021

Hi everyone, here I am with another article showing you how to develop a basic program in C#. This time I will show you how to create a Guess the Number game.

In this tutorial I am using Rider as my IDE. So you will need to create a C# console application on your chosen IDE.

Once you have done you should see a “static void main” method and this is where the majority of our code will go.

First things first, we need to create 4 variables. One which will contain the number the user is trying to guess between 1 and 50 you could set it to whatever, we will use the .Next() method and place a minimum and maximum value, another variable to contain the number of tries the user is allowed which in this case will be 10, thirdly, there will be a variable that contains the number of tries the user has made to guess the number, finally, the last variable will be a boolean variable to be used in the while loop.

int randomNumber = new Random().Next(1, 50);
int allowedTries = 10;
int numberOfTries = 0;
bool gameLoop = true;

The next step is to prompt the user to enter a number so we will write a simple Console.WriteLine.

Console.WriteLine("Please enter a number between 1 and 100...");

--

--

Responses (2)