INTRODUCTION
By now you should be getting pretty good at programming; we have now looked at basic output and basic maths. Now we are going to combine the two and add input!Resources:
IDEONE: www.ideone.com
If you need to check back on our other lessons:
Basics: http://scotechnologyofficial.blogspot.co.uk/2014/01/programming-why-not-give-it-go.html
Maths: http://scotechnologyofficial.blogspot.co.uk/2014/01/how-to-programming-episode-2-basic-maths.html
LESSON
Once again load up IDEONE and select VB.NET as your language of choice, you should now be able to do this without a problem. Delete the code there and retype it again to help you memorize it:Imports System
Public Class Test
Public Shared Sub Main()
Like last time we are going to dimension our variables, but we are not going to initialize them with any values as we did last time:
Dim integer_1 as Integer
Dim integer_2 as Integer
Dim result as Integer
Now we want to output some text to prompt the user to enter some information. For this program we will be adding two numbers together, so we will tell the user this.
Console.Writeline("Please enter first number to add: ")
Next we are going to try something new, we are going to take input from the user:
integer_1 = Console.Readline()
As you can see this is very similar to something we tried in our basic maths tutorial where we did this:
"Result = integer_1 + integer_2". To take input, the statement is very similar to the Console.Writeline statement, but instead of writing something to the console we are reading something into the console. We want the integer_1 variable to be set as our input so we read the input in to the variable. The console readline statement reads in any input you make from the next line and will wait until you press enter to take the input in. We can now repeat this process for integer_2:
Console.Writeline("Please enter second number to add:")
integer_2 = Console.Readline()
Now we can use our mathematical programming knowledge to add these two numbers and output the result to the screen:
result = integer_1 + integer_2
Console.Writeline("{0}",result)
End Sub
End Class
There is however one more thing that we can do to tidy this code up:
If you are not displaying any text in the output other than the contents of a variable then you do not have to waste time putting curly brackets in or the referencing number, all you need to do is write the variable name inside of the brackets without quotation marks like so:
Console.Writeline(result)
Bringing all this together, we should have the complete source code as follows:
Imports System
Public Class Test
Public Shared Sub Main()
Dim integer_1 as Integer
Dim integer_2 as Integer
Dim result as Integer
Console.Writeline("Please enter first number to add: ")
integer_1 = Console.Readline()
Console.Writeline("Please enter second number to add:")
integer_2 = Console.Readline()
result = integer_1 + integer_2
Console.Writeline(result)
End Sub
End Class
Looking like this:
Input on IDEONE is done by clicking the button on your code window marked: 'stdin'. Clicking on this will drop down a box allowing you to enter input, enter a number, press the return key and enter another number.
Once you have two numbers click the run button and you will see the result of adding the two numbers in the stdout box!
Once again thank you for reading:
Joseph Wright - SCOTECH
No comments:
Post a Comment