[英]why does my program return "CS0120: An object reference is required for the non-static field, method, or property 'Question3.primechec(BigInteger)'"?
im new to C# and ive been trying to solve project euler question number 3 and got the following error: i understand i need to decler a new BigInt before i use it, but i just cant figure out the syntax.我是 C# 的新手,我一直在尝试解决项目 euler 问题 3 并得到以下错误:我知道我需要在使用新的 BigInt 之前对其进行 decler,但我只是无法弄清楚语法。 would love to get some help!很想得到一些帮助!
using System;
using System.Numerics;
namespace primes
{
class Question3
{
static void Main()
{
BigInteger Memash = 600851475143 ;
for(BigInteger i = 100 ; i <= Memash ; i++)
{
if(primechec(i))
{
Console.Write("this number is prime: " + i);
if(Memash % i == 0)
{
Console.Write(i);
}
}
}
}
public bool primechec(BigInteger Naor)
{
for(int j = 2 ; j <= Naor ; j++)
{
if(Naor % j == 0)
{
return false;
}
}
return true;
}
}
}
The problem is that your Main
method is static
, so it can't call instance methods in the same class.问题是您的Main
方法是static
,因此它不能在同一个 class 中调用实例方法。 You need to declare primechec
static as well, so you're calling a static
method from a static
method.您还需要声明primechec
static,因此您从static
static
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.