Reputation: 1
Console.WriteLine("enter your number : ")
Dim number1, number2, number3, largest As Integer
number1 = 2
number2 = 7
number3 = 14
' if 2 > 0 then largest = 2
If number1 > largest Then largest = number1
' if 7 > 2 then largest = 7
If number2 > largest Then largest = number2
' if 14 > 7 then largest = 14
If number3 > largest Then largest = number3
label1.text = largest
Upvotes: 0
Views: 584
Reputation: 15091
I used a List(Of T)
. This is a class available in the .Net framework https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0 It is a bit like an array except you don't have to know the size before you start. You don't have to worry about indexes because the .Add
method will add at the end of the list. The T
stands for Type
, in our case, an Integer
.
Define a variable to hold the user input.
We want 5 numbers from the user so we set up a loop to run 5 times. (It could be i = 1 To 5
, just habit that I use 0 To 4
)
The message to write a number will appear 5 times. However, what if the user doesn't enter a number? Our list will only accept Integer
s. We must test the user input to make sure it is an Integer
. Use Integer.TryParse
https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__ We are using the first Overload.
.TryParse
is a very clever Function. It checks the String input, in our case, Console.ReadLine
, to see if it can be converted to an Integer
. If it can the Function returns True
and Our Do
loop will end. If False
the code inside the Do
loop will run and we ask the user to try again. Note that this does not effect the outer For loop so we will still get 5 numbers.
.TryParse
does one more thing. When it is successful converting the input String
to an Integer
, it puts the value into the second parameter, in our case, an Integer
called input
.
The last line of the For loop adds the successfully converted string to the list.
Finally we display the result to the user. I used and interpolated string indicated by the $
. This allows you to include variables or expressions right in the string as long as they are surrounded by braces. { }
We used an Extension Method of List(Of T)
, .Max
. You can see this listed in the link I gave you for List(Of T)
Sub Main()
Dim lst As New List(Of Integer)
Dim input As Integer
For i = 0 To 4
Console.WriteLine("Enter a whole number")
Do Until Integer.TryParse(Console.ReadLine, input)
Console.WriteLine("Sorry, not a whole number. Try Again.")
Loop
lst.Add(input)
Next
Console.WriteLine($"The highest number you entered is {lst.Max}")
Console.ReadKey()
End Sub
The explanation is longer than the code. Do check out the links. It is good to start to learn how to read Microsoft documentation. I always found the Remarks section and the examples most helpful. If you are working in vb be sure to select it with the first drop down arrow on the right side of the page. This will show the examples in vb.
Upvotes: 0
Reputation: 530
This is a C# example with comments. It will display you higest number entered. It does not keep all the numbers. If you like you like to store all number you can create an array to store them in.
int higestNumber = int.MinValue;//here the higest number willbe held, first value is lowest possible integer
int newNumber;//temp value for current number eneted by the user
for(int i = 0; i < 5; i++)//cycle to enter 5 numbers
{
Console.WriteLine("Enter number " + (i+1));// message to the user (i+1) so its starts from 1 and not 0
newNumber = int.Parse(Console.ReadLine());//entering number by the user
if (newNumber > higestNumber)//checking if the new number is higer then the old one
higestNumber = newNumber;//storing the new higer number
}
Console.WriteLine("Higest number is: " + higestNumber);//displaying higest number`
Here is a vb version as well
Private Shared Sub Main(ByVal args As String())
Dim higestNumber As Integer = Integer.MinValue 'here the higest number willbe held, first value is lowest possible integer
Dim newNumber As Integer 'temp value for current number eneted by the user
For i As Integer = 0 To 5 - 1 'cycle to enter 5 numbers
Console.WriteLine("Enter number " & i + 1) ' message to the user (i+1) so its starts from 1 and not 0
newNumber = Integer.Parse(Console.ReadLine()) 'entering number by the user
If newNumber > higestNumber Then higestNumber = newNumber 'checking if the new number is higer then the old one
'storing the new higer number
Next
Console.WriteLine("Higest number is: " & higestNumber) 'displaying higest number
End Sub
Upvotes: 0
Reputation: 460168
So you have a console app and want to ask the user for 5 integers and output the highest?
Dim numberList As New List(Of Int32)
Do
Dim nextNumber As Int32
Do
Console.writeline("Please enter an integer")
Loop Until Int32.TryParse(Console.ReadLIne(), nextNumber)
numberList.Add(nextNumber)
Loop Until numberList.Count = 5
' easiest way:
Dim maxNumber = numberList.Max()
' learning way:
maxNumber = numberList(0)
For Each num In numberList
If num > maxNumber Then maxnumber = num
Next
Console.WriteLine($"Max-Number is: {maxNumber}")
Upvotes: 1
Reputation: 156
Try some formula first.
Assign a default value as the largest value Let each value entered be tested against the default largest value If it is larger, then assign the value of the default to the entered value If it is smaller, ignore it and continue the input Your final value assigned to the default largest value will be the largest.
Upvotes: 1
Reputation: 73
You can use Math.Max
int number1 = 2, number2 = 7, number3 = 15;
label1.text = Math.Max(Math.Max(number1, number2), number3));
Upvotes: 0