下面是我的程序:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
    class Program
    {
        int age(int n)
        {
            int c;
            if(n==1)
                c=10;
            else
                c=age(n-1)+2;
            return (c);
        }        static void Main(string[] args)
        {
            
            int temp=age(5);
            
        }    }
}
编译提示一下错误,请问怎么解决?非静态的字段、方法或属性“ConsoleApplication2.Program.age(int)”要求对象引用

解决方案 »

  1.   

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.IO; 
    namespace ConsoleApplication2 

        class Program 
        { 
            int age(int n) 
            { 
                int c; 
                if(n==1) 
                    c=10; 
                else 
                    c=age(n-1)+2; 
                return (c); 
            }         static void Main(string[] args) 
            { 
                Program p=new Program();
                int temp=p.age(5); 
                
            }     } 

      

  2.   

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.IO; 
    namespace ConsoleApplication2 

        class Program 
        { 
           static  int age(int n) 
            { 
                int c; 
                if(n==1) 
                    c=10; 
                else 
                    c=age(n-1)+2; 
                return (c); 
            }         static void Main(string[] args) 
            { 
                
                int temp=age(5); 
                
            }     } 

      

  3.   

    private static int age(int n)
            {
                int c;
                if(n==1)
                    c=10;
                else
                    c=age(n-1)+2;
                return (c);
            } 
      

  4.   

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.IO; 
    namespace ConsoleApplication2 

        class Program 
        { 
            int age(int n) 
            { 
                int c; 
                if(n==1) 
                    c=10; 
                else 
                    c=age(n-1)+2; 
                return (c); 
            }         static void Main(string[] args) 
            { 
                Program p=new Program(); 
                int temp=p.age(5); 
       //调用一个类的方法需要使用该类的对象才可以         
            }     }