2016-04-29 36 views
1

我有我寫的代碼,其中有3個標籤用於颶風數量,平均颶風數量以及xt文件中颶風數量最多的年份。代碼正在工作,前兩個標籤顯示正確的結果。然而,最後一個標籤顯示的是颶風數量最多的一年,而不是年份。在標籤中輸入錯誤的輸出

以下是我有:

Option Strict On 

公共類frmHurricaneStatistics

' Class level Private variables. 
Public Shared _intSizeOfArray As Integer = 20 
Private _strYears(_intSizeOfArray) As String 
Private _intNumberOfHurricans(_intSizeOfArray) As Integer 

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

    ' This load event reads the inventory text file and fills 
    ' the ComboBox object with the Hurricane Statistics. 

    ' Initialize an instace of the streamreader object and declare variables. 
    Dim objReader As IO.StreamReader 
    Dim strHurricaneStatistics As String = "Hurricanes.txt" 
    Dim intCount As Integer = 0 
    Dim intFill As Integer 
    Dim strFileError As String = "The file is not available. Please restart the 
     application when the file is available." 

    ' Verify the Hurricane.txt file exists. 
    If IO.File.Exists(strHurricaneStatistics) Then 
     objReader = IO.File.OpenText(strHurricaneStatistics) 

     ' Read the file line by line until the file is completed. 
     Do While objReader.Peek <> -1 
      _strYears(intCount) = objReader.ReadLine() 
      _intNumberOfHurricans(intCount) = Convert.ToInt32(objReader.ReadLine()) 
      intCount += 1 
     Loop 
     objReader.Close() 

     ' The ComboBox objext is filled with the Years for Hurricanes. 
     For intFill = 0 To (_strYears.Length - 1) 
      cmbYears.Items.Add(_strYears(intFill)) 
     Next 
    Else 
     MsgBox(strFileError, , "Error") 
     Close() 

     ' If ComboBox is filled then enable the Display Statistics button. 
     ' btnDisplayStatistics.Enabled = True 
    End If 
End Sub 

Private Sub btnDisplayStatistics_Click(sender As Object, e As EventArgs 
             ) Handles btnDisplayStatistics.Click 

    ' This click event calls the sub procedures for the selected years and 
    ' the number of hurricans in that year. 
    Dim intSelectedYear As Integer 
    Dim strMissingSelection As String = "Missing Selection" 
    Dim strSelectAYearError As String = "Please Select a Year" 

    ' If the ComboBox object has a selection, Display Statistics. 
    If cmbYears.SelectedIndex >= 0 Then 
     intSelectedYear = cmbYears.SelectedIndex 
    Else 
     MsgBox(strSelectAYearError, , strMissingSelection) 
    End If 

    ' The procedure MakeLabelsVisible Is called to display the labels 
    ' And the results. 
    MakeLabelsVisible() 

    Dim intAverage As Double 
    Dim intYear As Integer 


    For intIndex As Integer = 0 To _intNumberOfHurricans.Length - 1 
     If intYear < _intNumberOfHurricans(intIndex) Then 
      intYear = _intNumberOfHurricans(intIndex) 
     End If 
     intAverage = intAverage + _intNumberOfHurricans(intIndex) 
    Next 

    intAverage = intAverage/_intNumberOfHurricans.Length 


    ' 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 Year " & 
     _strYears(intSelectedYear) & " is " & _intNumberOfHurricans(intSelectedYear).ToString() & "." 
    lblAvergeNumberHurricanes.Text = "The Average Number of Storms was " & FormatNumber(intAverage, 0) & " Hurricanes." 
    lblMostStorms.Text = "The Year " & intYear & " Had The Most Storms Between " & (
     _strYears(20) & " And " & (_strYears(0).ToString)) 




End Sub 




Private Sub MakeLabelsVisible() 

    ' This procedure displays the labels with the calculated results 
    lblNumberOfHurricanes.Visible = True 
    lblAvergeNumberHurricanes.Visible = True 
    lblMostStorms.Visible = True 




End Sub 

更新後的代碼。

回答

2

看起來你只是用颶風數量填充intYear?

intYear = _intNumberOfHurricans(intIndex) 

我看不到你想從哪裏得到一年的價值。有人甚至存在嗎?請張貼的代碼的其餘部分

編輯:

從我明白了什麼(糾正我,如果我錯了),你希望那有颶風最多的一年?如果是這樣

嘗試

For intIndex As Integer = 0 To _intNumberOfHurricans.Length - 1 
    If _intNumberOfHurricans(intIndex) = _intNumberOfHurricans.Max Then 
     intYear = Integer.Parse(_strYears(intIndex)) 
    End If 
    intAverage = intAverage + _intNumberOfHurricans(intIndex) 
Next 

我在做什麼這裏是_intNumberOfHurricans尋找最高值,並將其與當前迭代颶風的數量。如果它們是相同的,那麼我們是颶風數量最多的一年,所以我們用_strYears填充intYear(但是作爲整數)。

此代碼並不完美。例如,如果颶風的最高數量是100,但有兩年有100個颶風,它只會提供最新的一年,而不是第一年有100次颶風。

+0

您好我已經把其餘的代碼。我真的很感謝你的幫助 –

+0

更新了我的答案,讓我知道它是怎麼回事 – p3tch

+0

再次p3tch,我很抱歉成爲一個巨大的痛苦,但我輸入了代碼,因爲你已經建議,現在我得到一個格式執行。我即將放棄。畢竟,我的意思是多麼糟糕,可能會影響我的成績。大聲笑 –

1

因爲你設置了;

intYear = _intNumberOfHurricans(intIndex) 

不是這一年,颶風的數量。這應該指向一年的財產。

intYear = _intNumberOfHurricans(intIndex).Year 

希望有所幫助。

+0

嗨,再次。我非常感謝幫助。我添加了.year,並且我收到一個消息,那年並不是整數的成員。我只在我的第六週編程,所以我不知道如何解決這個問題。 –

+0

嗨,我認爲你使用的是oop,我的意思是你需要在你的類中添加Year屬性。如果您沒有,則不應將_intNumberOfHurricans(intIndex)變量設置爲intYear,因爲它不是正確的。代碼丟失,我不能告訴你更多,但我想你不是閱讀文本文件中的年份數據。 – Berkay

+0

Hi B.Yaylaci。我正在嘗試從文本文件中獲取年份數據。在txt文件中有20個條目。例如:2010年以後的颶風數量是17。多年來我一直在暗淡_stryears和_intNumberOfHurricans的數字。我的結果是顯示數量而不是年數。試圖訪問年度前我遇到了問題。 2005年,我只得到今年的數字,這是我的結果中的27歲 –