使用两个数组进行输出

Using two arrays for output

我想弄清楚如何根据列表框对象中的 selection 输出结果。就像我 select “2002”,我希望它输出那一年的总降雨量。我已经尝试通过堆栈和 google 进行研究,但我似乎找不到任何东西,除非我解释不正确。我希望数组根据它们的索引输出。这是必要的代码。

Public Class frmRainfall

    ' Class level Variables

    Dim strYearSelection() As String = {
        "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009",
        "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017"
    }

    Dim strYear As String

    ' Yearly stats found online due to issues finding text file

    Dim decYearlyRainfallTotals() As Decimal = {

        28.66, 37.56, 31.36, 41.78, 31.1, 35.44, 48.82, 38.95, 30.73, 38.44, 46.99,
        36.39, 48.26, 32.56, 48.5, 44.83, 45.18, 47.87
    }

    Dim intOneYearTotal As Integer
    Dim intOverallTotal As Integer
    Dim intAverage As Integer

    Private Sub frmRainfall_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'The Form at load will read the text file (Unavailable, read comment
        ' at the end of the frmRainfall sub procedure) and fill the ListBox object with the
        'Year items.

        For Each strYear In strYearSelection
            lstSelectYear.Items.Add(strYear)
        Next
    End Sub

这就是你想要的

首先,获取所选年份

Dim selectedYr as String = Convert.ToString(lstSelectYear.Text)

获取所选年份的索引和该索引中的沉淀。

Dim precip as Decimal = Convert.ToDecimal(decYearlyRainfallTotals(Array.IndexOf(strYearSelection, selectedYr)))

我想推荐一个不同的数据结构。键值对字典。它为您提供了您的月份和预付款之间的关系。

Dim dict as new Dictionary(Of String, String)

添加一条记录:

dict.Add("2000", 28.66)

获取所有年份

Dim allYears() as String = dict.Keys.ToArray()

获取一年的价值

Dim value as String = dict("2000")