Reputation: 167
I'm trying to come up with the code for this question:
Ask the user to enter ten numbers and then dislpay the largest one
I have so far come up with this, but looking at how it would be implimented, I can only see errors:
Sub Main()
Dim One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten As String
Console.WriteLine("Please enter your first number")
One = Console.ReadLine
Console.WriteLine("Please enter your second number")
Two = Console.ReadLine
Console.WriteLine("Please enter your third number")
Three = Console.ReadLine
Console.WriteLine("Please enter your fourth number")
Four = Console.ReadLine
Console.WriteLine("Please enter your fifth number")
Five = Console.ReadLine
Console.WriteLine("Please enter your sixth number")
Six = Console.ReadLine
Console.WriteLine("Please enter your seventh number")
Seven = Console.ReadLine
Console.WriteLine("Please enter your eighth number")
Eight = Console.ReadLine
Console.WriteLine("Please enter your nineth number")
Nine = Console.ReadLine
Console.WriteLine("Please enter your tenth number")
Ten = Console.ReadLine
If Ten > Nine Then
Console.WriteLine("Your biggest number is" & Ten)
Else
If Nine > Eight Then
Console.WriteLine("Your biggest number is" & Nine)
Else
I need to see what the biggest number is. Another pupil is using an array to achieve this, but I cannot seem to work them out yet.
So after the user enters the numbers, how can I achieve this?
I tried (above) using If statements, but I see the code being extensive and errors arising if a person enters a larger number at the beginning, and then one at the end, as it would display only the first larger number...
Upvotes: 1
Views: 5612
Reputation: 56
You don't need an array,
Console.Writeline("Number 1?")
Dim Number as integer = Console.Readline()
For Key as Integer = 2 to 10
Console.Writeline("Number " & Key & "?")
'Get current number and compare with larger number?
Next
Console.Writeline("Largest number: " & Number)
Read this and then try writing it yourself, you'll learn much more that way :)
Upvotes: 1
Reputation: 700472
There are mainly two approaches:
Put all numbers in an array and loop through them after all are input.
Keep track of the largest number during input, so that you compare each new number to the previous highest, and replace it if it is higher.
Upvotes: 1
Reputation: 43743
If you don't want to use arrays or collections, I would recommend only keeping the highest value after each input. For instance, here's some psuedo code:
WriteLine("Please enter your first number")
Current = ReadLine
If Current > Highest Then
Highest = Current
End If
... Do it again
Upvotes: 3