素数有0个自变量的问题
本文关键字:问题 自变量 0个 | 更新日期: 2023-09-27 18:22:06
我得到一个错误"当我在事件处理程序中调用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;
}
您的方法接受一个参数testNum
。如果在调用此方法时不传递它。发生编译时错误时说:
No overload for method 'IsPrime' takes 0 arguments
错误呼叫:
IsPrime(); //no argument is being passed
正确的调用方式:
IsPrime(3); //any integer can be passed