gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:36:26 25 4
gpt4 key购买 nike

所以我正在编写一个程序,该程序应该记录某人出生的年份,然后告诉他们他们的年龄以及有关他们出生年份的事实。所以我写了一个包含事实和信息的哈希表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 个单位长,但我留下了一些,以防问题出在我的操作方式上。除了事实和数字之外,它们都是相同的。

最佳答案

HashTable 采用一个 key,正如您从您调用的 Add(key, value) 方法中看到的那样。

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

Console.WriteLine(Years(x))

其中 x 是年份(或 key)。

关于vb.net - 我的哈希表正在反转其输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14149654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com