We are happy to announce that today you will create your second project in the Visual Studio environment! This will be a mobile application (Android). 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 in Mobile App (C#), create a Random Numbers Generator.

MATRIX team

Android app

Android app (C#)

PROJECT 2.2.3
Guess Number

Mobile App (Android)

 

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 Mobile app development tools in Visual Studio 2022 has not been fully developed by the Microsoft team, so we will use 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:

Random rnd = new Random();
'int nums = rnd.Next(1, 100);
FindViewById<EditText>(Resource.Id.editText1).Text = nums.ToString();

 

In the second line, we will specify random numbers ranges - from 1 to 99.

In thi third line we will put random number in text box. To put number in text box in C#, we have to convert it to string, so we are using .ToString() after the variable - nums.

 

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)
{
FindViewById<TextView>(Resource.Id.textView1).Text = "More";
}
else if (a<b)
{
FindViewById<TextView>(Resource.Id.textView1).Text = "Less";
}
else
{
FindViewById<TextView>(Resource.Id.textView1).Text = "Equal";
}

 

 

 

 

4. Int32.Parse and .ToString()

We are using Int32.Parse, 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(FindViewById<EditText>(Resource.Id.editText1).Text) > Int32.Parse(FindViewById<EditText>(Resource.Id.editText2).Text))

{

FindViewById<TextView>(Resource.Id.textView1).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 we have to deploy the App to the Android mobile phone. Please connect your PC to the phone using the USB cable. To be able deploy the app to the Android phone first we have to enable developer options. Here is the link that will help you>>>

After the developer option is allowed you will see your phone name in the Visual Studio menu panel (after the green triangle button).

Click the green triangle button on the menu panel to deploy the App to the phone. After it is done the App will run on the phone.

 

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

 

Conclusion:

We have created our second Android application in the Visual Studio environment using the Android App (Xamarin). We developed the 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 make a Random Numbers Generator.

We deployed our app on the phone, changing some phone parameters and connecting the phone to the PC.