误差:"一个对象引用是所必需的非静态字段,方法或属性..." [英] Error: "an object reference is required for the non-static field, method or property..."

查看:252
本文介绍了误差:"一个对象引用是所必需的非静态字段,方法或属性..."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林创建于C#的应用程序。它的功能是evualuate如果给定是素数,如果相同的交换数是素为好。当我建立我的Visual Studio解决方案,它说:对象引用是必需的非静态字段,方法或属性...。有林的volteado和siprimo的方法这个问题。
你能告诉我在哪里的问题,我怎么能解决这个问题?谢谢!

 命名空间的ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            Console.Write(写一个数字:);
            长= Convert.ToInt64(到Console.ReadLine()); //一个是由用户给定的数
            长的av = volteado(一); // AV是一,但换            如果(siprimo(一)==假放;&安培; siprimo(AV)== FALSE)
                Console.WriteLine(原件及交换数字是素数。);
            其他
                Console.WriteLine(一个数字的心不是主要的。);
            到Console.ReadLine();
        }
        私人布尔siprimo(长)
        {//评估如果所接收的数是素
            布尔SP = TRUE;
            为(长K = 2; K< = A / 2; k ++)
                如果(A%K == 0)SP = FALSE;
            返回SP;
        }
        私人长期volteado(长)
        {//交换收到的号码
            长V = 0;
            而(一个大于0)
            {
                V = 10 * V +一%10;
                一个/ = 10;
            }
            返回伏;
        }
    }
}


解决方案

您不能从静态方法访问非静态成员。 (请注意,的Main()是静态的,这是净的要求)。只要siprimo和volteado静态的,通过将static关键字在他们面前。例如:

 静态私人长期volteado(长)

Im creating an application on C#. Its function is to evualuate if a given is prime and if the same swapped number is prime as well. When I build my solution on Visual Studio, it says that "an object reference is required for the non-static field, method or property...". Im having this problem with the "volteado" and "siprimo" methods. Can you tell me where is the problem and how i can fix it? thank you!

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;
        }
    }
}

解决方案

You can't access non-static members from a static method. (Note that Main() is static, which is a requirement of .Net). Just make siprimo and volteado static, by placing the static keyword in front of them. e.g.:

 static private long volteado(long a)

这篇关于误差:&QUOT;一个对象引用是所必需的非静态字段,方法或属性...&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆