循环不循环和第二如果不是Iffing

时间:2014-05-16 21:39:19

标签: vb.net loops if-statement

这是我的所有代码

Module Module1

Sub Main()
    Dim yob As Integer
    System.Console.WriteLine("If you would as be kind as to input your year of birth then I will        tell you how old you are")
loops:
    Try
        yob = Int32.Parse(Console.ReadLine())
    Catch ex As SystemException
        Console.WriteLine("that is not a number, try again.")
        GoTo loops
    Finally
    End Try
    Do Until yob > 0 And yob < DateTime.Today.Year
    Loop
    If yob < 0 Or yob > DateTime.Today.Year Then
        Console.WriteLine("You entered a number that is either zero or a date in the future.")
        Console.WriteLine("You can't be born in the future. C'mon son")
        Console.WriteLine("Try again")
    End If
    Dim Age = DateTime.Today.Year - yob
    Console.WriteLine("You are " & Age & " years old")
    If Age > 100 Then
        Console.WriteLine("You are were born over a 100 years ago and little is known about that time since there was no Facebook, Twitter or Instagram for people to log their every")
        Console.WriteLine(" thought, action, emotion, or take pictures of their food before this time")
        Console.ReadKey()
    End If
    If Age > 90 Then
        Console.WriteLine("You were born in the 20s")
        Console.WriteLine("In this decade Halloween was born. The first Halloween celebration in America took place in Anoka, Minnesota in 1921. ")
        Console.ReadKey()
    End If
End Sub

End Module

直到我在try catch块中添加了一个goto,我的循环运行正常。现在它没有,如果你输入比2014年更大的一年它只是坐下来。 我遇到的另一个问题是我的第二个如果不起作用。如果你把一个年龄大于90岁的年份放入它没有做任何事情,程序关闭而不执行If语句。 关于什么是错的任何想法?

3 个答案:

答案 0 :(得分:2)

您要循环的代码必须位于Do循环中。

Do
Try
    yob = Int32.Parse(Console.ReadLine())
Catch ex As SystemException
    Console.WriteLine("that is not a number, try again.")
    GoTo loops
Finally
End Try
Loop Until yob > 0 And yob < DateTime.Today.Year

答案 1 :(得分:1)

哇,Goto。不是每天都能看到其中的一个,而且有充分的理由。你不应该使用它。在某些情况下它很有用,但你不可能真正遇到其中一个。

代码停止一年超出范围的原因是你有一个空循环来检查年份。由于变量不能在循环内部发生变化,循环将永远不会结束。

你应该在输入周围而不是在它之后使用循环,并在循环内进行所有检查。您可以使用TryParse方法而不是让代码抛出异常,通常最好检查导致错误的条件,而不是等待它发生:

Dim yob As Integer
System.Console.WriteLine("If you would as be kind as to input your year of birth then I will        tell you how old you are")
Dim inputting As Boolean = True
Do While inputting
  Dim input As String = Console.ReadLine()
  If Int32.TryParse(input, yob) Then
    If yob <= 0 Or yob > DateTime.Today.Year Then
      Console.WriteLine("You entered a number that is either zero or a date in the future.")
      Console.WriteLine("You can't be born in the future. C'mon son")
      Console.WriteLine("Try again")
    Else
      inputting = False
    End If
  Else
    Console.WriteLine("that is not a number, try again.")
  End If
Loop

当我测试一年使我成为94时,第二个If声明工作正常。但是,它只检查下限,所以如果我输入一年让我150岁,它仍然说我出生在二十几岁。

此外,在90到100之间并不意味着你出生在二十几岁,但是在85到95​​之间。您可能希望在该条件下使用实际出生年份而不是年龄:

If yob >= 1920 And yob <= 1929 Then

旁注:只有出生年份不足以准确确定年龄。如果我出生于1994年,我不确定我20岁,如果我今年没有过生日,我还是19岁。

答案 2 :(得分:1)

您的代码中存在两个问题。 (好吧,如果我算上goto,还有三个) 第一个是空的do while,如果你键入一个大于当前年份的值,它将永远循环,如上面的评论中所指出的那样。第二个问题是两个输出之间缺少else if。如果我有超过100年的时间,那么我也有超过90年的时间,你写下这两个陈述。

第一个问题是修复删除所有try / catch块和Do while循环

While True
    if Int32.TryParse(Console.ReadLine(), yob) then
      if yob > 0 And yob < DateTime.Today.Year then
        Exit While
      else
        Console.Writeline("Enter a number between 0 and " & DateTime.ToDay.Year)
      End if
    else
        Console.Writeline("This is not a number")
    End if
End While

请注意,TryParse比简单的Parse更好,因为如果输入不是数字,它将返回false并且不会抛出异常。

第二个问题只是修复添加适当的else if(并使用Console.Readline以确保在用户按下回车时退出)

If Age > 100 Then
    .....
    Console.ReadLine()
else If Age > 90 Then
    ....
    Console.ReadLine()
End If

但请记住,计算一个人的年龄并不是一项简单的任务 Look at this question