Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 我的哈希表正在反转其输出_Vb.net - Fatal编程技术网

Vb.net 我的哈希表正在反转其输出

Vb.net 我的哈希表正在反转其输出,vb.net,Vb.net,所以我正在写一个程序,这个程序需要一个人出生的一年,然后告诉他们他们有多大,以及他们出生的年份。所以我写了一个包含1950-2012年的事实和年龄的哈希表,现在我正试图让它们在某人进入一年后输出。现在它正在做的是计算年份,计算我想要得到的年龄,但它得到的是与哈希表正好相反的结果(例如,如果你说你出生在1950年,程序会给出应该给那些说他们的出生年份是2012年的人的回复,等等) 这是到目前为止的代码。我希望有一种方法可以解决这个问题,而不必从头开始 Imports System Imports

所以我正在写一个程序,这个程序需要一个人出生的一年,然后告诉他们他们有多大,以及他们出生的年份。所以我写了一个包含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个单元长,所以没有张贴一堵文本墙,但我留下了几个,以防问题在于我是如何做的。除了事实和数字之外,它们都是相同的。

哈希表
使用一个
,您可以从调用的
Add(key,value)
方法中看到这一点

要根据
键访问数据,请执行以下操作:

Console.WriteLine(Years(x))

其中x是年份(或
)。

为什么不创建一个包含所有事实的字符串数组?那你就做吧

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

那么如果我把所有的钥匙都换成年份而不是应该工作的年龄?去掉减法部分?我会这么做的。如果愿意,也可以直接删除
.Values
部分。。但(对我来说)用这一年作为关键更有意义