素数需要0个参数问题

时间:2013-07-13 01:18:58

标签: c# .net

我收到错误“方法没有重载'当我在事件处理程序中调用IsPrime时,IsPrime'需要0参数?

public bool IsPrime(int testNum)
{
    // return True if argument is prime number
    // return false otherwise

    // A prime number is a natural number that has exactly two divisors, 1 and the number its self.
    // 

    if (testNum == 1) return false; // by definition of Prime numbers, 1 is not a prime
    if (testNum == 2) return true; // short circuit out, we know that 2 is the first prime number

    for (int i = 2; i < testNum; ++i)  {
       if (testNum % i == 0)  return false;
    }

    return true;
}

1 个答案:

答案 0 :(得分:4)

您的方法需要一个参数testNum。如果在调用此方法时未传递它。发生编译时错误:

No overload for method 'IsPrime' takes 0 arguments

错误的电话:

IsPrime();  //no argument is being passed

调用此方法的正确方法:

IsPrime(3);  //any integer can be passed