2010-10-24 59 views
0

下面是我的C#作業本週的第5號草案。我首先使用Linq編寫了程序,並且它運行良好。不幸的是,方向聲明我必須創建自己的方法,而不是使用已經在Linq中找到的美妙Sum()方法。這個源代碼的主要問題是方法重載不正確(也可能是我的整個Sum()方法也是錯誤的)。由於我們全能的文本沒有清楚地解釋如何重載這樣的方法,我有點失落......(或者很多失敗)。錯誤CS1501:我沒有正確地重載Sum()方法

這裏是賦值指令(再一次):

「創建接受任意數量的整數參數,並顯示它們的總和)命名的總和(方法寫演示和的Main()方法()方法當傳遞一個,三個,五個或十個整數的數組時,它會正常工作。將該程序保存爲UsingSum.cs。「

從微軟的Visual C#®2008年,介紹了面向對象程序設計,3E,喬伊斯·法雷爾

這裏是我的源代碼:

using System; 

public class UsingSum 
{ 
    public static void Main() 
    { 

     //Step 1: Adding 1, 3 and 5 

     int[] array1 = { 1, 3, 5 }; 

     int a; 
     int b; 
     int c; 
     int d; 
     int e; 
     int f; 
     int g; 
     int h; 
     int i; 
     int j;   
     int firstSum; 
     int secondSum; 

     Console.Write("When the numbers 1, 3 and 5 are added together, using the Sum() method, the answer is: "); 

     firstSum = Sum(array1); 
     Console.WriteLine("{0}", firstSum); 



     //Step 2: Entering variables into Array2[10] 


     Console.Write("Enter first integer for addition: "); 
     a = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter second integer for addition: "); 
     b = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter third integer for addition: "); 
     c = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter forth integer for addition: "); 
     d = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter fifth integer for addition: "); 
     e = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter sixth integer for addition: "); 
     f = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter seventh integer for addition: "); 
     g = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter eighth integer for addition: "); 
     h = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter ninth integer for addition: "); 
     i = Convert.ToInt32(Console.ReadLine()); 
     Console.Write("Enter tenth integer for addition: "); 
     j = Convert.ToInt32(Console.ReadLine()); 

     int[] array2 = { a, b, c, d, e, f, g, h, i, j }; 

     Console.Write("The total of {0} + {1} + {2} + {3} + {4} + {5} + {6} + {7} + {8} + {9} is: ", 
     a, b, c, d, e, f, g, h, i, j); 

     secondSum = Sum(array2); 
     Console.WriteLine("{0}", secondSum); 


    } 


//Step 3: Defining the Sum() method 

    public static int Sum(int a, int b) 

//My overload is generating error CS1501: No overload for method 'Sum' takes '1' arguments 

    { 

    int sum = 0; 
    int[] adder = new int[0]; 
//designating an array with no parameters... 

    for(int a = 0; a < adder.Length; ++a) 
     adder[a] = a; 

    foreach(int b in adder) 
     sum += b; 
     Console.WriteLine(" " + sum); 
    } 
} 

回答

1

您正在定義總和取2個參數

public static int Sum(int a, int b) 

但只有1個參數

firstSum = Sum(array1); 
012叫它

嘗試定義琛採取的int數組:

public static int Sum(int[] input) 
+0

好吧,我給一個嘗試:-)謝謝你這麼清楚解釋問題。 – Nooob 2010-10-24 17:33:54

+0

它的工作! :-)我忘了在最後加上回報。實際的Sum函數對任何事物都產生0,但我會想出來......謝謝你的幫助。 – Nooob 2010-10-24 17:37:57

+0

...我的源代碼編譯和工作完美。它生成0是因爲我在foreach中引用了加法器數組(而在加法器中是int b)而不是引用用於重載Sum()方法的「輸入」數組。非常感謝所有幫助我在本週末推出這些東西的人們。 :-) – Nooob 2010-10-24 18:04:12