2016-07-06 9 views
-2

このコードにはいくつか問題があります。このプログラムに関する解決策を見つけるのを助けてください。関数内のパラメータ配列を動的に渡すこと

Module Module1 
Function AddElements(ParamArray arr As Integer()) As Integer 
    Dim sum As Integer = 0 
    Dim i As Integer = 0 
    For Each i In arr 
     sum += i 
    Next i 
    Return sum 
End Function 
Sub Main() 
    Dim sum As Integer 
    Dim k As Integer() 
    Dim j As Integer 
    Dim n As Integer 
    Console.WriteLine("Enter the Array Value") 
    n = Console.ReadLine() 
    For j = 1 To n 
     Console.WriteLine("Enter the value of:") 

     k(j) = Console.ReadLine() 

    Next 
    sum = AddElements(k(j)) 
    sum = Console.ReadLine() 

    Console.WriteLine("The sum is: {0}", sum) 


    Console.ReadLine() 
End Sub 

End Module 
+0

あなたAddElements関数に配列全体を単一要素のみを渡すとされていないように見えます。 – JRSofty

答えて

0
  1. あなたは持っていることになっているサイズのK()を初期化する必要があります。
  2. JRSoftyは現在、関数に配列ではなく単一の配列を渡していると言っています。
  3. sum = Console.ReadLine()のポイントは何ですか?合計を計算してから、コンソールからの入力で値を上書きします。

修正されたコード

Module Module1 
Function AddElements(ParamArray arr As Integer()) As Integer 
    Dim sum As Integer = 0 
    Dim i As Integer = 0 
    For Each i In arr 
     sum += i 
    Next i 
    Return sum 
End Function 
Sub Main() 
    Dim sum As Integer 
    Dim j As Integer 
    Dim n As Integer 
    Console.WriteLine("Enter the Array Value") 
    n = Console.ReadLine() 
    Dim k(n) As Integer '1 
    For j = 1 To n 
     Console.WriteLine("Enter the value of:")   
     k(j) = Console.ReadLine() 
    Next 
    sum = AddElements(k) '2 
    'sum = Console.ReadLine() '3 

    Console.WriteLine("The sum is: {0}", sum) 


    Console.ReadLine() 
End Sub 

End Module 
関連する問題