2014-03-13 21 views
1

我正在創建一個程序,根據他們輸入的日期查找一個人活着的年數,然後在他們活着的十年中給他們瑣事。但是,當我運行它,我得到一個錯誤,說明輸入日期不能轉換爲雙精度。我對此很新,所以任何幫助,將不勝感激。這裏是我的代碼:Years Lived /瑣事程序問題

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 
+0

請不要在標題中指定語言,標籤更好地服務於此目的。 :) – Crono

回答

0

改變每

firstDate < n 

...這個:

secondDate.Year < n 

執行相同的>和=操作了。

我也建議你改變這一行:

secondDate = CDate(firstDate) 

...進入這一行:

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

謝謝!這似乎已經完成了大部分。 – Trevb0b7

+0

你還剩下哪部分? – Crono

+0

什麼都不重要,只需添加一些評論,也許更多的信息'瑣事'部分。 – Trevb0b7

0

你不能用一個數字式直接比較的日期類型。然而,在你的情況下,看起來secondDate只是firstDate強制到Date類型。

因此,請在您的If語句中使用secondDate.Year

例如secondDate.Year < 1900