看板 Visual_Basic 關於我們 聯絡資訊
Public Class Form1 Dim index As Integer = 0 Dim numOfElement As Integer = 0 Dim numArray() As Integer Dim sum As Integer = 0 Dim average As Double Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If numOfElement = 0 Then numOfElement = CInt(TextBox1.Text) TextBox1.Text = "" Label2.Text = "" If numOfElement > 0 Then Dim numArray(numOfElement) As Integer End If Else If index < numOfElement Then numArray(index) = CInt(TextBox1.Text) TextBox1.Text = "" index = index + 1 End If End If Label1.Text = "輸入第" & CStr(index + 1) & "個數字" If index = numOfElement Then For index As Integer = 0 To numOfElement - 1 sum = sum + numArray(index) Next average = sum / numOfElement Label2.Text = CStr(numOfElement) & "個數字的平均值是" & CStr(average) Label1.Text = "輸入數字個數" numOfElement = 0 index = 0 sum = 0 End If TextBox1.Focus() End Sub End Class -- 可以執行~可是執行到紅色那裡就卡死 出現:第一個可能發生的例外狀況類型 'System.NullReferenceException' 發生於 WindowsApplication1.exe 然後我看了說明 他說要我加new 不過加了編譯後就告訴我陣列不能這樣搞 那....問題在哪裡? -- ▃▂▁ . 玄月王朝 御林軍團 威武校衛 ◤◥ 裡我的愛 ▔▔▔▔▔▔▔▔▔. ◢█ █◣ ;▌▎ ▃▂▁. ◥◥◤◤ ▆▄▆ FCK-MOON -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.130.198.4
m9556:numArray <<你有宣告array的範圍嗎?多檢查幾次吧? 01/13 20:00
fumizuki:sub中應該是redim preserve array(index) ... 01/15 06:39
fumizuki:否則numArray就是此sub中的區域變數 01/15 06:40