【发布时间】:2014-05-16 21:39:18
【问题描述】:
这是我所有的代码
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 年大的年份,它就坐在他们的位置上。 我遇到的另一个问题是我的第二个 If 不起作用。如果您输入的年份使您的年龄大于 90 它不会执行任何操作,并且程序会在不执行 If 语句的情况下关闭。 有什么想法吗?
【问题讨论】:
-
你调试了吗?设置一个断点并比较它实际执行的操作与您认为的执行操作
-
很抱歉,我对此很陌生,这是我的第一门编程课程。程序编译得很好,只是没有做它应该做的事情。
-
这就是为什么我们调试——修复逻辑错误;了解代码实际如何运行可以减少未来类似的错误
-
当你进入 2020 年时,
Do Until yob > 0 And yob < DateTime.Today.Year...Loop会是什么? -
它什么都不做。程序停止,如果不手动点击角落的退出键,您将无法继续或退出。
标签: vb.net loops if-statement