C# &引用;不包含静态';主要';“适用于入口点的方法”;
我不知道下面的代码是怎么回事 当我尝试编译时,我得到了以下信息: 不包含适合入口点的静态“main”方法 这是我的代码:C# &引用;不包含静态';主要';“适用于入口点的方法”;,c#,main,entry-point,C#,Main,Entry Point,我不知道下面的代码是怎么回事 当我尝试编译时,我得到了以下信息: 不包含适合入口点的静态“main”方法 这是我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; name
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace RandomNumberGenerator
{
public partial class Form1 : Form
{
private const int rangeNumberMin = 1;
private const int rangeNumberMax = 3;
private int randomNumber;
public Form1()
{
randomNumber = GenerateNumber(rangeNumberMin, rangeNumberMax);
}
private int GenerateNumber(int min,int max)
{
Random random = new Random();
return random.Next(min, max);
}
private void Display(object sender, EventArgs e)
{
switch (randomNumber)
{
case 1:
MessageBox.Show("A");
break;
case 2:
MessageBox.Show("B");
break;
case 3:
MessageBox.Show("C");
break;
}
}
}
}
谁能告诉我哪里出了问题。每个C#程序都需要一个入口点。默认情况下,新的c#Windows窗体项目在Program.cs文件中包含一个程序
类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StackOverflow6
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间堆栈溢出6
{
静态类程序
{
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}
您可能丢失或删除了它。我自己也有这个问题 我创建了一个winforms项目,决定重构我的代码,该项目现在将不包含UI,因此我删除了Program.cs和winforms文件,只是为了得到与您相同的错误
您需要重新添加上面提到的static void main()方法,或者进入项目属性并将“应用程序”选项卡中的输出类型更改为类库。您的项目必须创建为空项目。因此,输出类型显示为控制台应用程序。将它更改为类库,它应该可以工作我也经历过这种错误。我更改了位于“项目属性/应用程序”选项卡(输出类型:)中的下拉列表。最初选择的值是“类库”,但我改为“Windows应用程序”,发现了相同的错误。现在已解决。代码中的简单更改。
main方法应该是'main'(大写字母M)。后来,但对我来说,它是一个“控制台应用程序”项目类型,但文件属性中的“构建操作”有些设置为“无”。将其更改为“编译”,结果很好。可能是您从项目中删除了程序文件。只需添加Program.cs文件即可解决问题
Random Random=new Random();返回random.Next(最小值、最大值)代码>不要新建它。将random
实例设置为顶级(类成员)。您的项目中有Program.cs文件吗?如果没有,那就是缺少的。程序类在哪里?顺便说一句,如果你的问题与你的问题无关,那么这个主题就是。