简体   繁体   English

在数组中获取错误的输出

[英]Getting the wrong output in array

I have been trying to get the correct outcome from an array. 我一直在尝试从数组中获取正确的结果。 I have tried everything I can think of. 我已经尝试了所有我能想到的。 I am new to VB.Net and programming. 我是VB.Net和编程的新手。

I have a file on usb that I am getting an array from. 我在USB上有一个要从中获取阵列的文件。 the txtfile is set up to give me the years and the number associated with that year. txtfile的设置是为了给我年份以及与该年份关联的数字。 for example. 例如。 year 21 1 year 19 6 year 18 18 year 17 12 and so on. 21年1年19年6年18年18 18年17 12等。

I am trying to find the year with the greatest number and display it in a label. 我试图找到数量最多的年份并将其显示在标签中。

I am getting the year 21 because it is the highest, but I actually need the year with the highest associated number in this case it would be year 16 with 27 as the associated number. 我得到的年份是21年,因为它是最高的年份,但实际上我需要关联数字最高的年份,在这种情况下,它应该是16年,关联数字为27。

here are the parts of the code that I believe are assoiciated with it. 这是我认为与之相关的代码部分。 I have written to find it but I am getting the first year only which is actually year 21. 我已经写了找到它的书,但是我只有第一年,实际上是21年。

the first line or years is _strYears(_intSizeOfArray) as string 第一行或年份是_strYears(_intSizeOfArray) as string

the second line or number assoiciated with the years is _strNumberOfHurricanes(_intSizeOfArray as integer 与年份_strNumberOfHurricanes(_intSizeOfArray as integer的第二行或数字是_strNumberOfHurricanes(_intSizeOfArray as integer

option strict must be on for the assignment 必须启用严格选项

    Dim intAverage As Double
    Dim intYear As Integer
    Dim intMostYears As Integer = 0


    '   Calculate the Statistics and display the results
    For intIndex As Integer = 0 To _intSizeOfArray
        If intYear < _intNumberOfHurricans(intIndex) Then
            intYear = _intNumberOfHurricans(intIndex)
        End If
        intAverage = intAverage + _intNumberOfHurricans(intIndex)
    Next

    intAverage = intAverage / _intNumberOfHurricans.Length
    For intLoopCounter = 0 To _intSizeOfArray
        If _strYears(intMostYears) < _strYears(intLoopCounter) Then
            intMostYears = intLoopCounter
        End If
    Next



    '   Display the statistics for the Storm Average in the selected Year
    '   and the most active year within the range of year.
    lblNumberOfHurricanes.Text = "The Number of Hurricanes in the  " &
        _strYears(intChoiceSelected) & " is " & _intNumberOfHurricans(intChoiceSelected).ToString() & "."
    lblAvergeNumberHurricanes.Text = "The Average Number of Storms was " & FormatNumber(intAverage, 1) & " Hurricanes."
    lblMostStorms.Text = "The" & _strYears(intMostYears) & " with " & intYear & " Had The Most Storms Between " &
        (_strYears(20) & " And " & (_strYears(0)))

The results I am getting are the first two labels are correct and the last label is displaying year 21 which should 16, and 27 which is correct, and year 21 and year 1 which is correct. 我得到的结果是前两个标签正确,最后一个标签显示21年应该是16,而27是正确,以及21年和1年是正确。

please this assignment is due in 1 and 1/2 hours, i have been trying to get this all day. 请在1和1/2个小时内完成此作业,我一直在努力争取这一天。 if anyone can give me an idea of where this is wrong. 如果有人可以告诉我这哪里错了。 I would really appreciate it. 我真的很感激。

These 3 lines will do most of what you're trying above. 这3行将完成您在上方所做的大部分工作。 Since this is an assignment, I'm not sure if your teacher would allow you to use Linq, but since you haven't specified anything like that, this should give you some light for the future if nothing else: 由于这是一项作业,所以我不确定您的老师是否允许您使用Linq,但是由于您还没有指定类似的名称,因此,如果没有其他规定,这应该可以为您提供一些启发:

Imports System.Linq

Dim intAverage As Double = _intNumberOfHurricans.Average()
Dim intMostYears As Integer = _intNumberOfHurricans.IndexOf( _intNumberOfHurricans.Max())

Now you can display this info in your labels just like you're doing. 现在,您可以像执行操作一样在标签中显示此信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM