Visual Basic Maximum, Minimum and Mean numbers program

RAZER Gamer

Estimable
May 27, 2015
35
0
4,590
3
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
 

Wolfshadw

Splendid
Moderator


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
 

Wolfshadw

Splendid
Moderator


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
 

ExpertNovice

Distinguished
Mar 25, 2006
12
0
18,560
0
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".

 
Thread starter Similar threads Forum Replies Date
M Apps General Discussion 1
E Apps General Discussion 1
Rainfire Apps General Discussion 3
R Apps General Discussion 1
Cathroad Apps General Discussion 1
RAZER Gamer Apps General Discussion 3
M Apps General Discussion 1
K Apps General Discussion 2
I Apps General Discussion 6
MrAlaweey98 Apps General Discussion 1
M Apps General Discussion 5
S Apps General Discussion 3
A Apps General Discussion 1
T Apps General Discussion 2
V Apps General Discussion 3
V Apps General Discussion 1
G Apps General Discussion 3
G Apps General Discussion 3
N Apps General Discussion 1
2 Apps General Discussion 1

ASK THE COMMUNITY