Dim Total As integer Dim counter as integer Dim Intnum As String IntNum = Inputbox("End with a null string", "Total") Counter = 0 Do Until Intnum = "" If isnumeric(Intnum) then Counter = Counter + 1 Total = Total + convert.toDecimal(Intnum) Else Messagebox.show("Input Numbers Only", "Error") end if IntNum = Inputbox("End with a null string", "Total") loop Total = Total / Counter Total.tostring Messagebox.show(Total, "Average Grade")
Dim Total As integer Dim counter as integer Dim Intnum As String
The Dimension Statements in the beginning just stated that Variables were being made, and also what type of variable they were
IntNum = Inputbox("End with a null string", "Total") Counter = 0
This is stating that whatever is input in the input box that is being made will be Intnum's new Value. the counter is used in averages to divide by however many scores(or whatever is being averaged) were put into the system.
Do Until Intnum = "" If isnumeric(Intnum) then Counter = Counter + 1 Total = Total + convert.toDecimal(Intnum)
The Do loop in the beginning is helping us out by telling the program when it should end. By setting the program to loop until nothing is entered allows us to exit the program without hanging(Crashing). The second line is there to make sure that the input from the user is a valid type of input and helps again by saving it from crashing. The final line makes sure that the total is being added correctly. It is known as an Accumulator. Lets say i put in 50 first and then 40, if its not total = total + intnum, then intnum would just reset the value of total to 40. this says
50 = 50 + 40 instead of 50 = 40.
Else Messagebox.show("Input Numbers Only", "Error")
Messagebox.show makes the user know that they entered a wrong type of information by telling them in a lovely little pop up window. The program gets there because of the isnumeric and the else statements.
end if IntNum = Inputbox("End with a null string", "Total") loop
The end if Ends the running of the If statement that was created when the isnumeric command came into the program. The second line makes the inputbox from the first time pop up again because the first inputbox wasn't in the loop. The last line just tells the program to go back up to the line that starts with "Do While" or "Do Until".
Total = Total / Counter Total.tostring Messagebox.show(Total, "Average Grade")
The total is then set equal to itself divided by the number of times the counter was incremented. If you had 3 inputs, then the code would be Total / 3. Then Total is set to be a string so that it can be output in a messagebox instead of making a messy form with labels. the final line puts the average number into a pop up window that allows the user to see it. All this can be done on a form with only 1 button. It could be done on a form with no buttons if you wish, just double click on the form itself and it will start working when the form loads. Although it works on a form with one button, i would always make something that has an exit button and all the basics just so it doesn't become so complicated that only the programmer or other programmers could understand how to work it.
I Hope this helps, and if you need other Tutorials, please ask me by sending me a message from my profile, i have no problem writing these if you like them. Also could you please provide some constructive criticism on how I could make these Tutorials more informative and just better in general. Thank you.