Here is the problem from the \"Microsoft Visual Basic 2015 RELOADED 6th edition\
ID: 3815807 • Letter: H
Question
Here is the problem from the "Microsoft Visual Basic 2015 RELOADED 6th edition" book. Page 345, Chapter 6, exercise #25.
Open the solution file contained in the VbReloaded2015Chap06Average Solution folder. The calcButton_Click procedure should get from zero to five test scores from the user. Display the test scores in the scoresListBox. When the user has finished entering the scores, the procedure should calculate and display the average test score. Code the procedure. Save the solution and then start and test the application. Close the solution. (1, 2, 4–6, 8).
Also, I am a beginner at this that started with chapter 1 of this book so my knowledge in this is minute.
Here is the interface and code view:
For the code, the Options need to be: Option Explicit On, Option Strict On, Option Infer Off.
Thank you very much!
Explanation / Answer
Assuming you enter space separated numbers in the Scores text box which signify the scores, i.e, like "1 2 3 4" or "10 20 30 40 50", we can do the following.
Assuming the scores text box is named as "txtScores" and the average scores label is named as "averageScores", the following code should work In the method calcButton_Click:
Dim scores = txtScores.value
Dim numbers = scores.Split().Count()
Dim sum = scores.Split().Select(Function(n) Integer.Parse(n)).Sum()
averageScore.value = sum/numbers
If the names of these elements are different, use the corresponding names. This should work!