我正在用C#创建一个应用程序。它的功能是评估给定的数字是否为质数,以及交换后的数字是否也是质数。
当我在Visual Studio中构建我的解决方案时,它会提示“需要一个对象引用才能访问非静态字段、方法或属性……”。我在“volteado”和“siprimo”方法上遇到了这个问题。
问题出在哪里,我该如何解决?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write("Write a number: ");
long a= Convert.ToInt64(Console.ReadLine()); // a is the number given by the user
long av = volteado(a); // av is "a" but swapped
if (siprimo(a) == false && siprimo(av) == false)
Console.WriteLine("Both original and swapped numbers are prime.");
else
Console.WriteLine("One of the numbers isnt prime.");
Console.ReadLine();
}
private bool siprimo(long a)
{
// Evaluate if the received number is prime
bool sp = true;
for (long k = 2; k <= a / 2; k++)
if (a % k == 0) sp = false;
return sp;
}
private long volteado(long a)
{
// Swap the received number
long v = 0;
while (a > 0)
{
v = 10 * v + a % 10;
a /= 10;
}
return v;
}
}
}