CS5001:exe不包含适用于入口点的静态“Main”方法

时间:2014-02-23 04:10:30

标签: c#

using System;

public class Newton_Ralphson
{
    public double compute(double x,int n,double[] P)            // x, degree, coefficients 
    {
        double pol = 0;
        for (int i = 0;i < n;i++)
        {
            for (int j = 1; j < n - i; j++)
                x *= x;
                pol += P[n - i] * x;
        }
        pol += P[0];
        return pol;
    }

public double[] secondary_Pol(int n, double[] P)            //slope
{
    double[] secP = new double[n - 1];
    for (int i = 0; i < n - 1; i++) 
        secP[i] = P[i + 1];
    return secP;
}

public double Main()
{
    Console.WriteLine("Estimate the solution for a nth degree polynomial!!! ");
    int n = 0;
    Console.WriteLine("Enter the degree of the polynomials! ");
    n = Int32.Parse(Console.ReadLine());
    double[] coefficients = new double[n+1];

    for(int i = 0;i < n+1;i++)
    {
        Console.WriteLine("Enter the coefficients ");
        Console.Write("coefficients of x to the power of {0}: ", n - i);         //decending coefficients 
        coefficients[n-i] = Convert.ToDouble(Console.ReadLine());
    }


    Console.WriteLine("Initial value is: ");
    double xint = Convert.ToDouble(Console.ReadLine());

    Console.WriteLine("How many times does the function go through? ");
    int precision = Convert.ToInt32(Console.ReadLine());
    double polyValue = 0;

    for (int j = 0; j < precision; j++)
    {
        polyValue = compute(xint, n, coefficients);                              //y0 in y = slope(x-x0) + y0
        double slope = compute(xint, n - 1, secondary_Pol(n, coefficients));
        xint = (-polyValue / slope) + xint;
    }
    Console.WriteLine(xint);
    Console.ReadLine();
    return 0;
}

}

当我在行中添加'static'时:public static double Main() 出现了另一个错误

错误CS0120:非静态字段,方法或属性'Newton_Ralphson.compute(double,int,double [])'

需要对象引用 执行另外两个函数时出现

错误

3 个答案:

答案 0 :(得分:5)

请参阅C# entry point function

C#应用程序,Main()必须是入口点。

  

原因是因为这就是语言的设计者   决定寻找什么作为您的计划的切入点。   他们也可以使用完全不同的方法来查找   入口点,例如使用元数据,或实例化对象   你(这将需要一个无参数的构造函数)。另一个原因   命名为void main()是对来自用户的直观   其他语言。

使用:

public class Newton_Ralphson
{
    public static double compute(double x, int n, double[] P) // x, degree, coefficients 
    {
        double pol = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 1; j < n - i; j++)
                x *= x;
            pol += P[n - i]*x;
        }
        pol += P[0];
        return pol;
    }

    public static double[] secondary_Pol(int n, double[] P) //slope
    {
        double[] secP = new double[n - 1];
        for (int i = 0; i < n - 1; i++)
            secP[i] = P[i + 1];
        return secP;
    }

    public static void Main()
    {
        Console.WriteLine("Estimate the solution for a nth degree polynomial!!! ");
        int n = 0;
        Console.WriteLine("Enter the degree of the polynomials! ");
        n = Int32.Parse(Console.ReadLine());
        double[] coefficients = new double[n + 1];

        for (int i = 0; i < n + 1; i++)
        {
            Console.WriteLine("Enter the coefficients ");
            Console.Write("coefficients of x to the power of {0}: ", n - i); //decending coefficients 
            coefficients[n - i] = Convert.ToDouble(Console.ReadLine());
        }


        Console.WriteLine("Initial value is: ");
        double xint = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("How many times does the function go through? ");
        int precision = Convert.ToInt32(Console.ReadLine());
        double polyValue = 0;

        for (int j = 0; j < precision; j++)
        {
            polyValue = compute(xint, n, coefficients); //y0 in y = slope(x-x0) + y0
            double slope = compute(xint, n - 1, secondary_Pol(n, coefficients));
            xint = (-polyValue/slope) + xint;
        }
        Console.WriteLine(xint);
        Console.ReadLine();

    }
}

1 - 将public double Main()更改为public static void Main()

2 - 将public double[] secondary_Pol(int n, double[] P)更改为public static double[] secondary_Pol(int n, double[] P)

3 - 将public double compute(double x,int n,double[] P更改为public static double compute(double x, int n, double[] P)

答案 1 :(得分:3)

向所有方法添加静态。如果不提供对象引用,则无法从静态方法调用非静态方法。作为替代方法,您可以创建类的实例并调用方法:

var c = new Newton_Ralphson();
c.compute();

答案 2 :(得分:2)

在Visual Studio中创建新的C#控制台应用程序将向您显示您通常需要的内容:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args) // static with a void (or int) return type
        {
        }
    }
}

Main必须声明为static

double Main绝对是一个问题。请参阅what a C# main method needs上较旧的MSDN文章 - 特别是voidint返回类型。

用于args的

Main string[]参数是可选的 - 即可以省略。请参阅上述MSDN文章以进行确认。

最后,将public添加到Main的声明很好但不必要,通常会跳过。