Option Strict On
Option Explicit On
Module Module1
Sub Main()
Dim HowMany As Integer
Dim nameArray(HowMany) As String
Dim HourlyWageArray(HowMany) As Double
Dim HoursWorkedArray(HowMany) As Integer
Dim GrossPayArray(HowMany) As Double
Dim x As Integer = 0
Console.WriteLine("How many employees?")
HowMany = CInt(Console.ReadLine())
Do While x < HowMany
Console.WriteLine("Employee name: ")
nameArray(x) = CStr(Console.ReadLine())
Console.WriteLine("Hourly rate: ")
HourlyWageArray(x) = CDbl(Console.ReadLine())
Console.WriteLine("Hours worked: ")
HoursWorkedArray(x) = CInt(Console.ReadLine())
If HoursWorkedArray(x) <= 40 Then
GrossPayArray(x) = HourlyWageArray(x) * HoursWorkedArray(x)
ElseIf HoursWorkedArray(x) > 40 Then
GrossPayArray(x) = ((HoursWorkedArray(x) - 40) * (HourlyWageArray(x) * 1.5)) + (40 * HourlyWageArray(x))
End If
x = x + 1
Loop
Console.WriteLine("{0,12:c} {1,12} {2,12:c} {3,12:c}", nameArray, HourlyWageArray, HoursWorkedArray, GrossPayArray)
Console.ReadLine()
End Sub
End Module
在第17行,nameArray(x)= CStr的(到Console.ReadLine()),收到錯誤 「類型的未處理的異常 'System.IndexOutOfRangeException' 發生」爲什麼我的數組超出範圍?
無論什麼值I輸入對於HowMany,嘗試輸入第二個員工姓名時發生此錯誤。
想想這個: 'y = 2; z = x + y; x = 1;'爲什麼不行? – 2014-10-05 03:42:54
因爲x還沒有被聲明......但是我不能把x = 0放到循環裏面嗎? – 2014-10-05 03:44:10
是的,所以你在這裏遇到了同樣的事情。 'HowMany'默認爲0,然後在分配值之前使用它。 – 2014-10-05 03:47:18