将数组中的数字转换为包含VB

时间:2016-04-03 17:51:00

标签: arrays vb.net parameter-passing subroutine

因此,我需要让控制台应用程序向用户询问姓名,小时数和付费率。这些答案将存储在适当的数组中。我需要使用来自小时的信息并在子例程中包含的计算中支付数组。我试图通过我的教科书或在线找到某种暗示,但在这种情况下,我似乎无法找到任何可以帮助我的东西...

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

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

我得到的错误是:

  

"类型的价值' Double'不能转换为“1维”   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

2 个答案:

答案 0 :(得分:1)

调用函数时,参数不是数组,它们是数组内的双精度数。所以你的两个函数不应该有参数" ByVal array2()作为double",而应该只是"小时为double" (对于array1)和"率为double"。那时,你不需要i索引到数组中,因为你没有通过数组。

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

答案 1 :(得分:0)

你没有传递数组,你从数组传递一个值,小时(i)是数组小时i的双倍值