C-SHARP - 2.7 Exercise: User Interaction
Hello everyone and welcome to this new video. In previous videos, we saw together how to interact with the user and convert a string into an integer. In this video I will give you an exercise to put into practice the theory we have seen together. Let's go! So in this exercise, you will have to do some kind of calculator. Basically, as you can see here you must ask the user for a number and then you will have to ask him for a second one. The program will add these two numbers and will simply have to display the result of this addition. So much for this exercise if you want, review the previous videos to help you. I hope it's clear to you. Pause the video and see you right after for the correction. We now meet for the correction of the exercise I hope you have succeeded, otherwise follow the correction carefully with me. The objective of this exercise was to ask the user to enter 2 numbers, add them up and then display the result of this addition. So at first we had to make a Console.WriteLine() and between quotation marks we put "Enter a number: " and don't forget them; Then we will interact with the user so below we had to create a string variable then you can give it as a name numberOne for example then = and there the Console.ReadLine() function which allows the user to enter something Then I will do the same for the 2nd number I make a Console.WriteLine(), I put between the quotation marks "Enter a second number: " and don't forget them; Below I create a second variable of type string that I will call numberTwo then = then the Console function.ReadLine(); I will make a small summary of what I have done So first I made a Console.WriteLine() to tell the user to enter a number. Then I created a string variable that will store what the user will enter thanks to the Console.ReadLine() function. As I told you in previous videos. The Console.ReadLine function only supports strings that's why here I was forced to create string variable And then I repeated the same thing for the 2nd number. Now that we have stored the numbers that the user will enter, we must now use them. To do this, we will create an int variable that we will call addition. Then we put a = and there in this variable we will store the result of the addition of the two numbers that the user to enter however there is a small problem. If we add the two string variables, we saw previously that when we add 2 strings, they will concatenate, they will basically stick together, for example if the user types numbers like 15 and 10 the result will be 1510 and still it will not work because we cannot store a string in an int variable. So how are we going to remedy this? Well, we're going to use the conversion we saw in the previous video. To do this, we will write int. Parse and in parenthesis we will put the variable numberOne then we will put a + because we want to make an addition and finally we write int.Parse and in parentheses we will put the variable numberTwo So here what did I do? I created an addition variable of type int then I will assign it something. And what is this something? Here in fact I recover the value contained in numberOne and numberTwo. These values are basically string, so they are considered strings and not an integer. And with the int.Parse() function I will transform them into integer which will now allow me to add them and store this addition in the addition variable. And finally, we had to display the result of this addition. So we're going to make a Console.WriteLine() and between parentheses I put numberA + " + outside the quotes I put a + then I put the number variableTwo I make a + I put quotes I put a = and outside the quotes I do + addition and at the end don't forget them; I launch the program, "Between So we managed to ask the user for two numbers, add them up and then display them. That's all for this exercise, I hope you succeeded and that everything was clear to you! I'll see you next time in a new video.