【问题标题】:Loop not looping and 2nd If not Iffing循环不循环和第二个如果不是如果
【发布时间】: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 &gt; 0 And yob &lt; DateTime.Today.Year...Loop 会是什么?
  • 它什么都不做。程序停止,如果不手动点击角落的退出键,您将无法继续或退出。

标签: vb.net loops if-statement


【解决方案1】:

您要循环的代码必须在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

【讨论】:

  • 这没有任何改变。结果和以前一样。
【解决方案2】:

哇,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 岁的年份,它仍然说我是 20 岁出生的。

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

If yob >= 1920 And yob <= 1929 Then

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

【讨论】:

  • 要准确确定确切的年龄,我必须考虑闰年等。我将简单地将我的 Console.Writeline 更改为“您今年已经或将要达到年龄”。
  • @TimTheLearner:如果您以天或周为单位计算年龄,您只需要考虑闰年,否则如果今年没有生日,您只需从年龄中减去一个。更改消息将准确地告诉程序进行了哪些计算,因此如果您不想将其更改为也接受生日,这是最好的解决方案。
【解决方案3】:

您的代码中有两个问题。 (好吧,如果我也算上goto,三个) 第一个是空的do while,如果你输入一个大于当前年份的值,它将像上面的 cmets 中指出的那样永远循环。第二个问题是两个输出之间缺少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 确保在用户按 Enter 时退出)

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

但是请记住,计算一个人的年龄并不是看起来那么简单的任务。
Look at this question

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2017-11-22
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多