c# - 如何创建适合入口点的静态“主要”方法?
问题描述
我最近开始使用 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();
}
}
}
解决方案
当控制台应用程序启动时,操作系统会调用该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
数组中。
这里有关于该方法的更多细节Main
:Main() 和命令行参数。您会在该文章的左侧看到它只是一系列文章中的一篇。您可以继续阅读以了解有关控制台应用程序如何工作的更多信息。
推荐阅读
- python - 将一系列值随机插入 pd.dataframe
- java - 当 CPU 使用率为 100% 时,使用 Shiro 登录过程需要太多时间
- python - 图像的 Python 图像
- javascript - 如果对象返回 true,则设置 ngModel
- r - 如何在 R 中绘制概率密度函数?
- javascript - 从文本框 id 中选择值到 PHP
- javascript - 创建构建时,我的应用程序是否会上传到 expo?
- typescript - 如何将cdk镜像资产部署到ecs集群
- r - 如何在 Rstudio 中创建分支的 git 分支以分别处理各个功能
- php - 如何将数组的第一个值发送到 php telegram bot 中的 sendmessage 方法?