我的HashTable正在扭转其产出

时间:2013-01-04 00:57:25

标签: vb.net

所以我正在编写一个程序,该程序应该花一年的时间才有人出生,然后告诉他们他们的年龄和他们出生的那一年的事实。所以我写了一个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个单位长,但我留下了一对,以防问题出在我是怎么做的。除了事实和数字之外,它们都是相同的。

2 个答案:

答案 0 :(得分:2)

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

要根据您要执行的key(年)访问数据:

Console.WriteLine(Years(x))

其中x是年份(或key)。

答案 1 :(得分:0)

为什么不用你的所有事实创建一个字符串数组呢?那你就可以做到

Dim facts as String() = {"fact0", "fact1", ...}
Dim Y as String = facts(2012 - x) 'Y is your value