'nested For loop and handle error with try...catch in visual basic
I try to run this code but the inner loop doesn't work
Sub Main()
Dim intyrs As Integer
Dim sngtotalRain, sngrain As Single
Dim i, j, flag, flag1 As Integer
flag = 1
flag1 = 1
sngtotalRain = 0
Console.WriteLine("Enter the number of years to use for the average rainfall: ")
While flag = 1
Try
intyrs = Console.ReadLine()
Console.WriteLine("")
For i = 1 To intyrs
Console.WriteLine("For year" & i)
While flag1 = 1
For j = 1 To 12
Console.WriteLine("Enter the rainfall amount for month " & j & " : ")
Try
sngrain = Console.ReadLine()
Catch
Console.WriteLine("Please enter amount of rainfall")
End Try
sngtotalRain += sngrain
Next
flag1 = 0
End While
Next
flag = 0
Catch
Console.WriteLine("Please enter numerical value") 'Showing the error for not inputting numeric values.
End Try
End While
Console.WriteLine("")
Console.WriteLine("For " & intyrs * 12 & " Total months, the average rainfall is " & sngtotalRain / intyrs / 12 & ", and the Total rainfall is " & sngtotalRain & " inches")
Console.ReadKey()
End Sub
End Module
When I enter a w, I want to go back month 2:
Enter the number of years to use for the average rainfall: 1
For year1 Enter the rainfall amount for month 1 : 1 Enter the rainfall amount for month 2 : w Please enter amount of rainfall Enter the rainfall amount for month 3 :
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
