我的计划有什么问题?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool check = isPowerOfTwo(255);
Console.WriteLine(check);
Console.ReadKey();
}
public bool isPowerOfTwo (uint x)
{
while (((x % 2) == 0) && x > 1)
{
x /= 2;
}
return (x == 1);
}
}
Run Code Online (Sandbox Code Playgroud)
}
我收到了错误
非静态字段,方法或属性需要对象引用.
使方法isPowerOfTwo静态:
public static bool isPowerOfTwo (uint x)
Run Code Online (Sandbox Code Playgroud)
方法Main是静态的,因此您只能在其中调用同一类的静态方法.但是,isPowerOfTwo由于它当前是一个实例方法,因此只能在Program类的实例上调用它.当然,你可以Program在里面创建一个类的实例Main并调用它的方法,但这似乎是一个开销.
| 归档时间: |
|
| 查看次数: |
2941 次 |
| 最近记录: |