Visual Basic Maximum, Minimum and Mean numbers program

Status
Not open for further replies.

RAZER Gamer

Reputable
May 27, 2015
703
0
5,360
I am currently working on a Visual Basics program to work out the Maximum, Minimum and Mean of the numbers inputted. I currently have it working with up to 50 numbers but I cannot get the Mean to work.

The other problem I have is that I want to be able to enter an infinite amount of numbers and type "END" or "-999" into the console to stop entering the numbers. At the moment you are asked how many numbers you want to enter at the start.

Here is the code:

Imports System.Console
Imports System.Threading
Module Module1

Sub Main()
BackgroundColor = ConsoleColor.White
ForegroundColor = ConsoleColor.Black
Clear()
Dim Max As Integer
Dim Min As Integer
Dim Mean As Integer
Dim NumberWanted As Integer
Dim UserInput As String
Dim i As Integer
Dim NumberOK As Boolean
WriteLine("--------------------------------------------------------------------------------")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| ------------------------------- |")
WriteLine("| {Max and Min number calculator} |")
WriteLine("| ------------------------------- |")
WriteLine("| Press Enter to continue: |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("| |")
WriteLine("--------------------------------------------------------------------------------")
WriteLine("Produced and developed by Microsoft Copyright © Microsoft 2015")
WriteLine("--------------------------------------------------------------------------------")
ReadLine()
Clear()
WriteLine(" ------------------------------ ")
WriteLine(" Max and Min number calculator: ")
WriteLine(" ------------------------------ ")
WriteLine()
WriteLine("How many numbers you want to type?")
Do
NumberOK = True
Try
NumberWanted = ReadLine()
If NumberWanted < 1 Then
WriteLine("Error: Number < 1, please enter a number between 1-50.")
NumberOK = False
End If
If NumberWanted > 50 Then
WriteLine("Error: Number > 50, please enter a number between 1-50.")
NumberOK = False
End If
Catch ex As Exception
WriteLine("Error: Please enter a number. ")
NumberOK = False
End Try
Loop Until NumberWanted > 0 And NumberOK = True
Thread.Sleep(1000)
WriteLine("Please enter " & NumberWanted & " numbers of your choice.")
For i = 1 To NumberWanted
WriteLine("Please enter number " & i & ":")
UserInput = ReadLine()
If i = 1 Then
Max = UserInput
Min = UserInput
ElseIf UserInput > Max Then
Max = UserInput
ElseIf UserInput < Min Then
Min = UserInput
End If
Thread.Sleep(100)
Next i
Clear()
WriteLine(" ------------------------------ ")
WriteLine(" Max and Min number calculator: ")
WriteLine(" ------------------------------ ")
WriteLine()
WriteLine("Max = " & Max)
WriteLine("Min = " & Min)
WriteLine("Mean = " & Mean)
WriteLine("Thank you for using the Max and Min number calculator!")
WriteLine("This programm will close in 15 seconds :)")
Thread.Sleep(15000)
End Sub
End Module


If anybody has any ideas or suggestions please reply to this thread.

Thanks
 
Solution


In this section of code, add the following lines:


For i = 1 To NumberWanted
WriteLine("Please enter number " & i & ":")
UserInput = ReadLine()
If i = 1 Then
Max = UserInput
Min = UserInput
ElseIf UserInput >...


In this section of code, add the following lines:


For i = 1 To NumberWanted
WriteLine("Please enter number " & i & ":")
UserInput = ReadLine()
If i = 1 Then
Max = UserInput
Min = UserInput
ElseIf UserInput > Max Then
Max = UserInput
ElseIf UserInput < Min Then
Min = UserInput
End If
MeanTotal = MeanTotal + UserInput
Thread.Sleep(100)
Next i
Mean = MeanTotal / i

Don't forget to declare MeanTotal.

As for allowing infinite an infinite number of entries, change your loop statement to end when UserInput < 0

-Wolf sends
 
Solution
Mean should not be an integer. Take two values 1 + 2 and the mean is 1.5 and not 1.


Stopping. Consider generalizing the routine. My suggestion is to stop when no value is entered and ask if they are done. If yes, then stop. To do that accept a variant and check for length of zero. Also, check for non-numeric values before assigning to an integer variable.


Wolf answered the hard part. However, if you generalize the routine then don't use "For i = 1 to NumberWanted". Instead use another loop construct, such as "Do until" loop. The until is when the user has entered a non-numeric value AND said they were ready to stop. Increment the counter "i" only when a numeric value has been entered.

Beyond.... if you are learning to code take a look at naming conventions. A quick glance SUGGESTS the Leszynski naming convention which is a variant of the Hungarian notation is commonly used for VB. Read about the reason for naming conventions and you will understand. thus, "FOR i = 1 to NumberWanted" might become "FOR lngEntries = 1 to intNumberWanted".

 
Status
Not open for further replies.