Hey guys, when i doing my VB, i get stuck on the try catch end try...i dono where should i put the try catch end try for the question..this is my question

Using loop statement, write a VB .NET program that prompts TWENTY (20) integers in 

the range of 1 to 100 (both inclusive) from the user in input boxes, determines and prints
the largest and smallest values in a message box.

i done my coding until here :

Dim x As integer
Dim num As integer
Dim max As integer
Dim min As integer

X = 0

Do while (X < 20)
num = Inputbox("Enter integer")
If x = 1 Then
max = num
min = num
else
End If
If num > max then
max = num
End if
If num < min then
min = num
End if
Next
MessageBox.Show ("The largest number is " & max)
MessageBox.Show ("The smallest number is " & min)
End
End sub
End class

Recommended Answers

All 9 Replies

Try
'All ur code goes here
Catch ex As Exception
' Cathc the error if any and show the user or log
End Try

okie~but how i catch when only wan user key in integer from 0-100 only? if the user key 140 it prompts invalid input

try
if (num < 100)
CODE
Catch
msgbox("INVALID NUMBER")

I believe it's something like that, hope this helps. :9

You can start by setting the values of min and max outside the loop as follows

Dim min As Integer = Integer.MaxValue
Dim max As Integer = Integer.MinValue

InputBox returns a string so you can't assign it directly to a numeric variable. You have to convert it. However, just because a string is numeric that doesn't mean it can be safely converted and assigned. For example, the string "999999999999999999999" is numeric but you can't convert it to a 32 bit integer. So you'll still need the Try-Catch. You want to minimize the code in the structure so put it inside the loop.

Try
    num = CInt(InputBox("Enter Integer"))
    'update max and min here
Catch ex As Exception
    MsgBox("oops")
End Try

Sir Jim, i cant understand ur try catch..what is the update max and min here mean? And where the try catch i should put at? can put inside my code there? so i can look at it easier and undestand easier too...thx anyway~

What Jim means is where you do thos "if's" (if num>max) and so on... they go where he says "update max and min here".

As in

Try
    num = CInt(InputBox("Enter Integer"))
    If num > max Then max = num
    If num < min Then min = num
    x += 1
Catch ex As Exception
    MsgBox("oops")
End Try

And by the way, x is almost never a good choice for a descriptive variable name.

how do i define that the number must be 0-100 only? i was stuck at here

I solve all the problem ready guys~thank for all the information~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.