We are happy to announce that today you will create your second project in the Visual Studio environment! This will be a Web application. We will use C# as a Programming Language.

Today, you will create the game "Guess Number". You will learn how to use "If-else If-else" constructions, create a random numbers generator

We will create the same project using different programming languages (VB.NET, C#, C++, Python, Java Script) and on different platforms (Desktop app, Web App, Mobile App, Arduino project). This is what makes the MATRIX an unique programming learning methodology.

MATRIX team

C#(Web App)

PROJECT 2.2.2
Guess Number

 

For our projects we will use Microsoft Visual Studio 2019. It allows to create projects in different programming languages and on different platforms.

At the time of creating our resource, the functionality of New Visual Web Developer in Visual Studio 2022 has not been fully developed by the Microsoft team, so we will use Visual Studio 2019.

Download and install Visual Studio 2019 on your Personal Compiuter.
Download Visual Studio 2019 >>>

Our Online Simulator will help you to make the game "Guess Number".

Now a little theory for our project:

1. Random numbers

To create a random generator we will use the next code:

if (TextBox1.Text == "")
{
Random rnd = new Random();
int nums = rnd.Next(1, 100);
TextBox1.Text = nums.ToString();
}

 

Here, the Random rnd = new Random(); is for getting the different numbers each time. Without this line, every time we run the code, we will get the same random numbers (it means they will not be truly random).

int nums = rnd.Next(1, 100); is for getting random numbers from 0 to 99. After adding +1 we will get random numbers from 1 to 100.

We put the random generator code between if { } because in the case of the web app when we click the button each time, the page will reload, so the number we have to guess will change while playing. To prevent this, we wrote the code that will generate a random number only if the textbox is empty:
if (TextBox1.Text == "")
So after the random number is generated it will not change during the game.

 

 

 

 

 

3. If - else if - else

You know how to use the If operator. else if is for adding one more condition. else means that code written after else will run if none of the above conditions is right.

Example:

If (a>b)
{
Label2.Text = "More";
}
else if (a<b)
{
Label2.Text = "Less";
}
else
{
Label2.Text = "Equal";
}

 

4. Int32.Parse and .ToString()

We are using Int32.Parse in front of TextBox.text, to tell the program text in the text boxes are numbers (Integer), so it is possible to compare them or perform other mathematical operations (We will learn about all types of variables later).

Example:

if (Int32.Parse(textBox2.Text)> Int32.Parse(textBox1.Text))
{
Label2.Text = "More";
}

 

To put number in text box in C#, we have to convert it to string, so we are using .ToString() after the variable.

 

Now it is time to start the simulation. Repeat the stages the simulator shows you in your Visual Studio.

 

Conclusion:

You have created the Web Application - game "Guess Number" in the Visual Studio environment with the C# programming language. At first, we worked on the program interface (visual side), after we wrote the programming code.

Now you know how to use If-else if-else construction and Random Numbers generators in the Web Applicationas