【发布时间】:2010-03-24 03:37:31
【问题描述】:
我正在用 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;
}
}
}
【问题讨论】:
-
欢迎来到 StackOverflow!以后请使用更具描述性的问题名称:)
-
请编辑您的问题以进行格式化。代码格式未涵盖代码的前几行。
-
制作相关格式,标题编辑:)
标签: c#