多年的生活/琐事计划问题

时间:2014-03-13 16:54:57

标签: .net vb.net

我正在创建一个程序,根据他们输入的日期查找一个人活着的年数,然后在他们活着的十年中给他们琐事。但是,当我运行它时,我得到一个错误说输入日期无法转换为双打。我对此很新,所以任何帮助都会受到赞赏。这是我的代码:

Module Module1

    Sub Main()
        Console.WriteLine("When were you born? Expressed dd/mm/yyyy, please.")

        Dim firstDate, msg As String
        Dim secondDate As Date
        firstDate = Console.ReadLine()

        Try
            secondDate = CDate(firstDate)
            msg = "Years from today (Rounded up, of course!): " & DateDiff(DateInterval.Year, Now, secondDate)
            Console.WriteLine(msg)
        Catch
            Console.WriteLine("That's not how I asked you to write the date!")
        End Try

        If firstDate < 1900 Then
            Console.WriteLine("...How are you even alive? Nice job!")
        ElseIf firstDate > 1900 And firstDate < 1910 Then
            Console.WriteLine("You're pretty old. You've seen a few world wars and the Russian Revolution--that's pretty sweet! Your age, I mean, not so much the wars...")
        ElseIf firstDate > 1910 And firstDate < 1920 Then
            Console.WriteLine("You probably grew up in the Roaring Twenties--Gatsby? What Gatsby?")
        ElseIf firstDate > 1920 And firstDate < 1930 Then
            Console.WriteLine("Well you likely missed the whole Roaring Twenties thing for learning to walk, but hey, at least you got the Great Depression! Oh...")
        ElseIf firstDate > 1930 And firstDate < 1940 Then
            Console.WriteLine("Looks like you were just young see the Second World War. And if you weren't lucky, fight in it.")
        ElseIf firstDate > 1940 And firstDate < 1950 Then
            Console.WriteLine("You were born in the same decade as Bruce Lee. 'Nuff said.")
        ElseIf firstDate > 1950 And firstDate < 1960 Then
            Console.WriteLine("You're from the same age as Bill Murray and Jay Leno.")
        ElseIf firstDate > 1960 And firstDate < 1970 Then
            Console.WriteLine("You're about the same age as my parents. How does that make you feel?")
        ElseIf firstDate = 1969 Then
            Console.WriteLine("Nice.")
        ElseIf firstDate > 1970 And firstDate < 1980 Then
            Console.WriteLine("You were a teenager when the Challenger space shuttle exploded.")
        ElseIf firstDate > 1980 And firstDate < 1990 Then
            Console.WriteLine("You grew up in the Eighties. I'm so sorry.")
        ElseIf firstDate > 1990 And firstDate < 2000 Then
            Console.WriteLine("Say 'Ninties Kids!' again! I dare you!")
        ElseIf firstDate > 2000 And firstDate < 2010 Then
            Console.WriteLine("Aren't you a little young for this sort of thing?")
        ElseIf firstDate > 2010 Then
            Console.WriteLine("Either your parents are doing this for you, or you're FROM THE FUTURE!!! Please be the latter.")
        End If

        Console.ReadKey()
    End Sub

End Module

2 个答案:

答案 0 :(得分:0)

更改每个

firstDate < n

......进入这个:

secondDate.Year < n

为&gt;做同样的事情和=运营商。

我还建议您更改此行:

secondDate = CDate(firstDate)

......进入这一行:

secondDate = DateTime.ParseExact(firstDate, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture)

答案 1 :(得分:0)

您无法直接将日期类型与数字类型进行比较。但是,在您的情况下,似乎secondDatefirstDate强制转换为Date类型。

因此,请在secondDate.Year语句中使用If

e.g。 secondDate.Year < 1900