2016-04-03 7 views
1

したがって、コンソールアプリケーションにユーザーに名前、時間、および支払いを依頼する必要があります。これらの回答は、適切な配列に格納されます。私は時間からの情報を使用し、サブルーチンに含まれる計算で配列を支払う必要があります。私は教科書やオンラインで何らかのヒントを見つけようとしましたが、この状況で私を助けるものは見つけられませんでした...VBからの計算を含むサブルーチンに配列から数値を取得する

私はこのエラーを理解していません。

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

私が取得エラーです:型 『ダブル』の値が

『ダブルの1次元 配列』に変換することができません

」私のプログラムは完全に動作しますサブルーチンの代わりにここに計算式を入れたとき

ここに私のコードの全体があります。

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 

答えて

1

関数を呼び出すと、パラメータは配列ではなく、配列の中に2つあります。したがって、あなたの2つの関数は、 "ByVal array2()as double"というパラメータを持つべきではありませんが、代わりに "hours as double"(array1)と "rate as double"その時点で、あなたは配列を渡さないので、配列にiのインデックスは必要ありません。

そして、関数内で1行に、「配列2(I)」を使用していない、単に「率」を使用し、代わりに「ARRAY1(I)」のちょうどあなたのパラメータ名を使用して「時間」

+0

したがって、それぞれを読みたい両方の配列の数?私はあなたが作った提案を試みましたが、私はまだ同じエラーが発生しています... – cryosneasel

+0

実際に私は問題を把握しました。私は関数にサブルーチンを変更し、正常に動作します。私たちの先生は、それがサブルーチンだと言っているサブルーチンのコードを教えてくれました。しかたがない。私は今働くようになった。ご協力ありがとうございました! – cryosneasel

0

配列を渡すのではなく、配列の値を渡しています。hours(i)は、配列の時間から要素iのdouble値です。

関連する問題