-6

我收到错误消息“当我在事件处理程序中调用 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;
}
4

1 回答 1

4

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

No overload for method 'IsPrime' takes 0 arguments

错误调用:

IsPrime();  //no argument is being passed

调用它的正确方法:

IsPrime(3);  //any integer can be passed
于 2013-07-13T01:29:50.087 回答