将数组中的数字放入包含 VB 中的计算的子例程

Getting numbers from an array into a subroutine that contains a calculation in VB

所以我需要让控制台应用程序询问用户姓名、工作时间和工资率。这些答案将存储在适当的数组中。我需要在子例程中包含的计算中使用小时数和工资率数组中的信息。我试图通过我的教科书或在线找到某种提示,但在这种情况下我似乎找不到任何对我有帮助的东西......

我不明白我在以下几行中遇到的错误。

total = overtimePay(hours(i), rate(i))
total = regularPay(hours(i), rate(i))

我得到的错误是:

"Value of type 'Double' cannot be converted into a '1-dimentional array of Double'

当我把计算方程而不是子程序放在这里时,我的程序完全可以运行

这是我的全部代码。

Module FinalAssignment1


Sub Main()


    'Author: Russell Peryy
    'Date: 4/2/16
    'Purpose: User enters info and program outputs the entered pay and calculated info

    'Declare constants
    Const author As String = "Russell Perry =================== Final Assignment 1"
    Const lines As String = "===================================================="

    'Declare Arrays
    Dim names(0 To 10) As String
    Dim hours(0 To 10) As Double
    Dim rate(0 To 10) As Double
    'Dim total(0 To 10) As Double


    'Declare variabels
    Dim i As Integer = 0
    Dim total As Double = 0

    'Display constants
    Console.WriteLine(author)
    Console.WriteLine(lines)
    space(1)

    'Get user information to fill name, hours, and rate array
    For i = 0 To 9 Step 1
        Console.Write("Enter employee's last name >> ")
        names(i) = Console.ReadLine()

        Console.Write("Enter employee's hours worked >> ")
        hours(i) = Console.ReadLine()

        Console.Write("Enter the employee's pay rate >> ")
        rate(i) = Console.ReadLine()

        'i = i + 1
        space(1)
    Next

    space(1)
    Console.WriteLine(lines)

    'Print info to screen
    For i = 0 To 9 Step 1
        If hours(i) >= 40 Then
            total = overtimePay(hours(i), rate(i))
            'total = (40 * rate(i)) + ((hours(i) - 40) * rate(i) * 1.5)
        Else
            total = regularPay(hours(i), rate(i))
            'total = hours(i) * rate(i)
        End If

        Console.WriteLine(names(i) & " worked " & hours(i) & " at a rate of " & String.Format("{0:C}", rate(i)) & " an hour for a total pay of " & String.Format("{0:C}", total))

    Next i

    'Pause the screen
    space(1)
    Console.WriteLine(lines)
    space(1)
    Console.Write("Press any key to exit >> ")
    Console.ReadKey()

End Sub

'subroutine for adding spaces
Sub space(ByVal x As Integer)
    For counter = 0 To x
        Console.WriteLine()
    Next
End Sub

'Subroutine for regular pay
Sub regularPay(ByVal array1() As Double, ByVal array2() As Double, ByVal i As Integer)
    Dim t As Double = array1(i) * array2(i)
End Sub

'subroutine for overtime
Sub overtimePay(ByVal array1() As Double, ByVal array2() As Double, ByVal i As Integer)
    Dim total As Double = ((40 * array2(i)) + ((array1(i) - 40) * array2(i) * 1.5))
End Sub
End Module

调用函数时,参数不是数组,而是数组中的双精度值。所以你的两个函数不应该有参数 "ByVal array2() as double",而应该只是 "hours as double"(对于 array1)和 "rate as double"。到那时,您将不需要数组中的 i 索引,因为您没有传递数组。

然后在函数内的一行中,不要使用 "array2(i)",只需使用 "rate",而不是 "array1(i)",只需使用您的参数名称 "hours"

您没有传递数组,您传递的是数组中的一个值,hours(i) 是数组 hours 中元素 i 的双精度值