首页 > 解决方案 > 如何创建适合入口点的静态“主要”方法?

问题描述

我最近开始使用 C# 中的方法,并且不断收到错误消息“程序不包含适合入口点的静态 'Main' 方法”。我尝试将一些静态 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();
        }

    }
    }

这是我尝试使用 System; 添加静态 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#methodsstatic-methods

解决方案


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

你所拥有的是一个类的一堆方法和字段,但没有办法让任何东西开始运行你的代码。

我很确定你想要你的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();
        }
    }
}

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

static void Main(string[] args)

命令行中的所有参数都将在args数组中。

这里有关于该方法的更多细节MainMain() 和命令行参数。您会在该文章的左侧看到它只是一系列文章中的一篇。您可以继续阅读以了解有关控制台应用程序如何工作的更多信息。


推荐阅读