"Top-level statements must precede namespace and type declarations"



所以我没有很长时间编码,所以我不是那么有经验,我最近在replit.com上遇到了一个问题,控制台会打印出:

error CS8803: Top-level statements must precede namespace and type declarations.
using System;

谁能提出问题?下面是我的代码,供任何想知道的人使用:

int English;
int Science;
int AverageCalc;
AverageCalc = Convert.ToInt32(Console.ReadLine());
class Program {
public static void Main (string[] args) {
Console.WriteLine("Write your math grades");
Math = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Write your english grades");
English = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Write your science grades");
Science = Convert.ToInt32(Console.ReadLine());
AverageCalc = (Math+English+Science/3);
}
}
if (AverageCalc > 80)
{
Console.WriteLine("You passed with A mark!");
}
else if (AverageCalc < 80)
{
Console.WriteLine("You passed with B mark!");
}
else if (AverageCalc < 65)
{
Console.WriteLine("You passed with C mark!");
}
else if (AverageCalc < 60)
{
Console.WriteLine("You passed with D mark!");
}
else if (AverageCalc < 55)
{
Console.WriteLine("You got lower than D mark, try better next time.");
}

正如@Caius在他的回答中提到的,你正在修复代码中的顶级语句和经典方式。

你可以按照他建议的方法,或者直接从你的代码中删除下面的部分。

class Program
{
public static void Main (string[] args) 
{

关闭Program类的}Main方法。

取自文档中的示例。

以下代码同

using System;
namespace Application
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

TLS

Console.WriteLine("Hello, World!");

您混合了顶级语句和非tls。TLS本质上允许您去掉所有的namespace/class/static main,只编写一个c#程序,就好像它是Main方法的内容一样:learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/

本质上,这意味着程序结构有点不稳定,因为你开始使用一些TLS,然后使用更常规的风格,然后返回TLS;从TLS结构切换(我建议你只是习惯名称空间/类/主的绒毛;它仍然被大量使用,在某种程度上,它是对花括号和作用域的合理介绍)。它看起来像:

namespace X{
class Program {
public static void Main (string[] args) {

int English;
int Science;
int AverageCalc;

Console.WriteLine("Write your math grades");

Math = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Write your english grades");

English = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Write your science grades");

Science = Convert.ToInt32(Console.ReadLine());

AverageCalc = (Math+English+Science/3);


if (AverageCalc > 80)
{
Console.WriteLine("You passed with A mark!");
}
else if (AverageCalc < 80)
{
Console.WriteLine("You passed with B mark!");
}
else if (AverageCalc < 65)
{
Console.WriteLine("You passed with C mark!");
}
else if (AverageCalc < 60)
{
Console.WriteLine("You passed with D mark!");
}
else if (AverageCalc < 55)
{
Console.WriteLine("You got lower than D mark, try better next time.");
}
Console.WriteLine("Press ENTER to exit (hah)");
Console.ReadLine();
}
}
}

这样一个简单的程序完全在Main方法的花括号范围内执行。之后,当你开始实际的面向对象的东西,并有多个学生的成绩跟踪,你会把东西从静态Main中移出来,写在其他花括号块中,但现在这样做就可以了。

您忘记为math声明一个变量-我将把它留给您作为练习来整理。此外,当变量在方法中声明时(Main是一个方法),您应该使用camelCase而不是PascalCase来命名它们。它现在看起来可能不重要,但这是惯例,当代码变得更复杂时,遵循它会有所帮助。PascalCase通常用于公共方法、属性、类和命名空间,而camel用于私有或本地。

简而言之,您的变量应该命名为englishscienceaverageCalc

c#要求所有的代码都在一个方法中,并且所有的方法都在一个类.

的代码
int English;
int Science;
int AverageCalc;
AverageCalc = Convert.ToInt32(Console.ReadLine());

就在那里,编译器不知道该怎么处理它。编译器期望首先调用Main()方法,所有其他代码必须从那里调用。

请记住,c#不是脚本语言其中语句从上到下逐个求值。

你不需要再声明程序类和主方法默认在最新版本中,你已经在主方法中,下面将工作

int English;
int Science;
int Maths;
int AverageCalc;
//AverageCalc = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Write your math grades");
Maths = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Write your english grades");
English = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Write your science grades");
Science = Convert.ToInt32(Console.ReadLine());
AverageCalc = (Maths + English + Science / 3);
if (AverageCalc > 80)
{
Console.WriteLine("You passed with A mark!");
}
else if (AverageCalc < 80)
{
Console.WriteLine("You passed with B mark!");
}
else if (AverageCalc < 65)
{
Console.WriteLine("You passed with C mark!");
}
else if (AverageCalc < 60)
{
Console.WriteLine("You passed with D mark!");
}
else if (AverageCalc < 55)
{
Console.WriteLine("You got lower than D mark, try better next time.");
}
Console.ReadLine();

最新更新