如何创建适用于入口点的静态“ Main”方法?

问题描述 投票:-1回答:1

我最近开始使用C#编写方法,并且不断收到错误消息:“程序不包含适用于入口点的静态“主”方法”。我尝试将某些静态void方法更改为Main,但它不起作用。我也尝试添加一个静态void Main(),但它也不起作用。如果这是一个愚蠢的问题,我表示歉意,我只是刚刚开始使用的方法。

using System;

namespace L07_Req1_FunMethods
{
    public class Conversions

    {
  static void Write()
        {
            Console.Write("Enter the number of degrees Farenheit you would like to convert to Celcius: ");
        }
        static int far = Convert.ToInt32(Console.ReadLine());
        static double cel = .555555555 * (far - 32);
        static void fToC()
        {
            Console.WriteLine("{0} degrees Farenheit is equal to {1} degrees Celcius.", far, cel);

            Console.Write("Now enter the number of Celcius you would like to convert to Farenheit: ");
        }
        static int cel2 = Convert.ToInt32(Console.ReadLine());
        static double far2 = (cel2 + 32) / .5555555555;
        static void cToF()
        {
            Console.WriteLine("{0} degrees Celcius is equal to {1} degrees Farenheit.", cel2, far2);

            Console.ReadKey();
        }

    }
    }

这是我尝试添加静态void Main()的代码使用系统;

namespace L07_Req1_FunMethods
{
    public class Conversions

    {
static void Main()
{

}
  static void Write()
        {
            Console.Write("Enter the number of degrees Farenheit you would like to convert to Celcius: ");
        }
        static int far = Convert.ToInt32(Console.ReadLine());
        static double cel = .555555555 * (far - 32);
        static void fToC()
        {
            Console.WriteLine("{0} degrees Farenheit is equal to {1} degrees Celcius.", far, cel);

            Console.Write("Now enter the number of Celcius you would like to convert to Farenheit: ");
        }
        static int cel2 = Convert.ToInt32(Console.ReadLine());
        static double far2 = (cel2 + 32) / .5555555555;
        static void cToF()
        {
            Console.WriteLine("{0} degrees Celcius is equal to {1} degrees Farenheit.", cel2, far2);

            Console.ReadKey();
        }

    }
    }
c# methods static-methods
1个回答
0
投票

很显然您是这个新手:)

当控制台应用程序启动时,操作系统将调用Main()方法。这就是为什么它抱怨它不存在。这就是所谓的“入口点”。这是操作系统开始运行代码的方式。

您拥有的是一堆方法和一个类的fields,但没有任何方法可以开始运行代码。

我很确定您想要Main方法中的所有代码,例如:

namespace L07_Req1_FunMethods
{
    public class Conversions
    {
        static void Main()
        {
            Console.Write("Enter the number of degrees Farenheit you would like to convert to Celcius: ");
            int far = Convert.ToInt32(Console.ReadLine());

            double cel = .555555555 * (far - 32);
            Console.WriteLine("{0} degrees Farenheit is equal to {1} degrees Celcius.", far, cel);

            Console.Write("Now enter the number of Celcius you would like to convert to Farenheit: ");
            int cel2 = Convert.ToInt32(Console.ReadLine());
            double far2 = (cel2 + 32) / .5555555555;
            Console.WriteLine("{0} degrees Celcius is equal to {1} degrees Farenheit.", cel2, far2);

            Console.ReadKey();
        }
    }
}

主要方法可以具有不同的签名。 method signature是指返回类型和参数的组合。例如,如果您想从命令行将参数传递到程序中,则可以像下面这样创建Main方法:

static void Main(string[] args)

并且来自命令行的所有参数都将在args数组中。

这里有关于Main方法的更多详细信息:Main() and command-line arguments。您会在该文章的左侧看到它只是一系列文章中的一个。您可以继续阅读以了解有关控制台应用程序如何工作的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.