3

所以我正在编写一个程序,它应该花费某人出生的一年,然后告诉他们他们的年龄以及他们出生年份的事实。所以我写了一个 Hashtable,其中包含 1950 年的事实和年龄-2012,现在我正试图让他们在有人进入一年时输出。现在它正在做的是计算年份,并进行我试图得到的计算,以及正确的年龄数字,但它与哈希表直接相反(例如,如果你说你出生于 1950 年,那么程序将给出它应该给那些说他们的出生年份是 2012 年等的人的响应)。

这是到目前为止的代码。我希望有一种方法可以解决它,而不必从头开始。

Imports System
Imports System.Collections

Module Module1
  Sub Main()
    Console.WriteLine("This program tells you your age based off of your birth year, and gives you a fact about that year. Please note, this year does not account for the recent year change to 2013 due to the majority of the work on it being done prior to 2013")
    Dim Years As New Hashtable
    Years.Add(0, "You are most likely less than 1 year old, your birth year (2012) was the year that the US Embassy in Lybia was attacked, leaving the US ambassador dead.")
    Years.Add(1, "You are most likely 1 year old, your birth year (2011) was the year that Osama Bin Laden, the master mind behind the September 11th attacks, was killed by Seal Team 6 in Pakistan.")

    Console.WriteLine("Please input the year of your birth.")
    Dim x As Integer
    Dim Y As Integer
    Try
        x = Console.ReadLine
    Catch ex As InvalidCastException
        Console.WriteLine("Please input a year between 1950 and 2012, the program will not work with an empty number.")
        End
    End Try
    Y = 2012 - x
    Console.WriteLine(Years.Values(Y))
    Console.ReadKey()
  End Sub
End Module

我删除了大部分哈希表,因为哈希表有 63 个单位长,所以不发布一堵文字墙,但我留下了几个,以防问题在于我是如何做的。除了事实和数字之外,它们都是相同的。

4

2 回答 2

2

HashTable需要 a key,您可以从Add(key, value)您正在调用的方法中看到。

要根据key(年份)访问数据,您将执行以下操作:

Console.WriteLine(Years(x))

其中 x 是年份(或key)。

于 2013-01-04T01:07:29.197 回答
0

为什么不直接创建一个包含所有事实的字符串数组?然后你可以做

Dim facts as String() = {"fact0", "fact1", ...}
Dim Y as String = facts(2012 - x) 'Y is your value
于 2013-01-04T01:16:26.330 回答