vb.net - Loop not looping and 2nd If not Iffing -
here's code
module module1 sub main() dim yob integer system.console.writeline("if kind input year of birth tell how old are") loops: try yob = int32.parse(console.readline()) catch ex systemexception console.writeline("that not number, try again.") goto loops end try until yob > 0 , yob < datetime.today.year loop if yob < 0 or yob > datetime.today.year console.writeline("you entered number either 0 or date in future.") console.writeline("you can't born in future. c'mon son") console.writeline("try again") end if dim age = datetime.today.year - yob console.writeline("you " & age & " years old") if age > 100 console.writeline("you born on 100 years ago , little known time since there no facebook, twitter or instagram people log every") console.writeline(" thought, action, emotion, or take pictures of food before time") console.readkey() end if if age > 90 console.writeline("you born in 20s") console.writeline("in decade halloween born. first halloween celebration in america took place in anoka, minnesota in 1921. ") console.readkey() end if end sub end module
until added goto in try catch block loop run fine. doesn't , if input year bigger 2014 sits their. other problem have 2nd if doesn't work. if put in year makes age bigger 90 doesn't , program closes without performing if statements. ideas on what's wrong?
wow, goto
. not everyday see 1 of those, , reason. shouldn't use it. there situations it's useful, not encounter 1 of them, ever.
the reason code stops year out of range have empty loop check year. variable can't change inside loop, loop never end.
you should use loop around input instead of after it, , checking inside loop. can use tryparse
method instead of letting code throw exception, it's better check condition cause error instead of waiting happen:
dim yob integer system.console.writeline("if kind input year of birth tell how old are") dim inputting boolean = true while inputting dim input string = console.readline() if int32.tryparse(input, yob) if yob <= 0 or yob > datetime.today.year console.writeline("you entered number either 0 or date in future.") console.writeline("you can't born in future. c'mon son") console.writeline("try again") else inputting = false end if else console.writeline("that not number, try again.") end if loop
when test year makes me 94, second if
statement works fine. however, checks lower bound, if enter year makes me 150 years old, still says born in twenties.
also, being between 90 , 100 doesn't mean born in twenties, being between 85 , 95 does. might want use actual birth year in condition instead of age:
if yob >= 1920 , yob <= 1929
side note: birth year not enough accurately determine age. if born 1994 it's not 20 years old, if haven't had birthday yet year, i'm still 19.
Comments
Post a Comment