【问题标题】:"Top-level statements must precede namespace and type declarations"“顶级语句必须在命名空间和类型声明之前”
【发布时间】:2021-10-17 05:42:08
【问题描述】:

所以我没有长时间编码所以我不是那么有经验,我最近在 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.");
}

【问题讨论】:

标签: c#


【解决方案1】:

您正在混合使用顶级语句和非 TLS。 TLS 本质上允许您取消所有 namespace/class/static main,只需编写一个 C# 程序,就好像它是 Main 方法的内容:docs.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 通常用于公共方法、属性、大小写和命名空间,而骆驼用于私有或本地。

简而言之,您的变量应称为englishscienceaverageCalc

【讨论】:

  • 谢谢,虽然我想要一个完整的解释,为什么它会显示错误信息
  • 编辑到答案的开头
【解决方案2】:

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

您可以按照他建议的方法,或者只是从您的代码中删除以下部分。

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

并关闭 }Program 类和 Main 方法。

示例取自documentation

以下代码相同。

using System;

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

TLS

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

【讨论】:

    猜你喜欢
    • 2022-10-01
    • 2011-07-06
    • 2020-09-03
    • 2015-09-10
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    相关资源
    最近更新 更多