错误CS1501:我没有正确地重载Sum()方法 [英] Error CS1501: I'm not overloading a Sum() method correctly

查看:111
本文介绍了错误CS1501:我没有正确地重载Sum()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是本周C#作业的第5号草稿。我首先使用Linq编写了程序,并且运行良好。不幸的是,这些指示指出我必须创建自己的方法,而不是使用Linq中已经存在的Sum()方法。此源代码的主要问题在于方法重载是不正确的(而且我的整个Sum()方法也可能是错误的)。由于我们的全能文字并未明确说明如何重载这样的方法,因此我有点迷失了……(或迷失了很多)。

Below is draft number 5 for my C# Homework this week. I wrote the program out using Linq first, and it worked fine. Unfortunately, the directions state that I must create my own method instead of using the wonderful Sum() method already found in Linq. The major problem with this source code is that the method overload is incorrect (and it's also probable that my entire Sum() method is wrong too). Since our almighty text doesn't clearly explain how to overload a method like this, I'm kind of lost... (or a lot lost).

这是赋值指令(再次):

Here are the assignment instructions (again):

创建一个名为Sum()的方法,该方法可以接受数量的整数参数并显示它们的和。编写Main()方法演示Sum()方法在传递一个,三个,五个或10个整数数组时正确工作。将该程序另存为UsingSum.cs。

"Create a method named Sum()that accepts any number of integer parameters and displays their sum. Write a Main()method that demonstrates the Sum()method works correctly when passed one, three, five, or an array of 10 integers. Save the program as UsingSum.cs."

来自Microsoft®Visual C#®2008,《面向对象编程入门》,3e,Joyce Farrell

from Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell

我的源代码:

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);
   }
}


推荐答案

您正在定义Sum接受2个参数

You are defining Sum to take 2 arguments

public static int Sum(int a, int b)

但只能用一个参数调用它

but only calling it with 1 argument

firstSum = Sum(array1);

尝试定义Sum以采用整数数组:

Try defining Sum to take an int array:

public static int Sum(int[] input)

这篇关于错误CS1501:我没有正确地重载Sum()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆