I faced this Compile time error in C# on Main method: A namespace cannot directly contain members such as fields or methods

huangapple go评论66阅读模式
英文:

I faced this Compile time error in C# on Main method: A namespace cannot directly contain members such as fields or methods

问题

using System;

namespace Calculator {
  class Program {
    public static void Addition(int a, int b) {
      int result = a + b;
      System.Console.WriteLine("加法结果是 {0}", result);
    }
    public static void Subtraction(int a, int b) {
      int result = a - b;
      System.Console.WriteLine("减法结果是 {0}", result);
    }
    public static void Multiplication(int a, int b) {
      int result = a * b;
      System.Console.WriteLine("乘法结果是 {0}", result);
    }
    public static void Division(int a, int b) {
      int result = a / b;
      Console.WriteLine("除法结果是 {0}", result);
    }
  }
  static void Main(string[] args) {
    Console.WriteLine("输入第一个数字 ");
    int num1 = int.Parse(Console.ReadLine());
    Console.WriteLine("输入第二个数字 ");
    int num2 = int.Parse(Console.ReadLine());
    Console.WriteLine("输入操作符 +, -, *, /");
    string op = Console.ReadLine();

    if (op.Equals("+")) {
      Program.Addition(num1, num2);
    } else if (op.Equals("-")) {
      Program.Subtraction(num1, num2);
    } else if (op.Equals("*")) {
      Program.Multiplication(num1, num2);
    } else if (op.Equals("/")) {
      Program.Division(num1, num2);
    } else {
      Console.WriteLine("操作符无效");
    }

    Console.ReadLine();
  }
}
英文:
using System;

namespace Calculator {
  class Program {
    public static void Addition(int a, int b) {
      int result = a + b;
      System.Console.WriteLine("Addition result is {0}", result);
    }
    public static void Subtraction(int a, int b) {
      int result = a - b;
      System.Console.WriteLine("Subtraction result is {0}", result);
    }
    public static void Multiplication(int a, int b) {
      int result = a * b;
      System.Console.WriteLine("Multiplication result is {0}", result);
    }
    public static void Division(int a, int b) {
      int result = a / b;
      Console.WriteLine("Division result is {0}", result);
    }
  }
  static void Main(string[] args) {
    Console.WriteLine("Enter first number ");
    int num1 = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter Second number ");
    int num2 = int.Parse(Console.ReadLine());
    Console.WriteLine("Enter operation symbols +, -, *, /");
    string op = Console.ReadLine();

    if (op.Equals("+")) {
      Program.Addition(num1, num2);
    } else if (op.Equals("-")) {
      Program.Subtraction(num1, num2);
    } else if (op.Equals("*")) {
      Program.Multiplication(num1, num2);
    } else if (op.Equals("/")) {
      Program.Division(num1, num2);
    } else {
      Console.WriteLine("Operation symbol is invalid");
    }

    Console.ReadLine();
  }
}

答案1

得分: 0

只需将 Division 方法和 Main 方法之间的一个大括号移到文件的末尾即可。代码将如下所示(我已格式化以更好地显示):

using System;

namespace Calculator
{
    public class Program
    {
        public static void Addition(int a, int b)
        {
            int result = a + b;
            System.Console.WriteLine("Addition result is {0}", result);
        }

        public static void Subtraction(int a, int b)
        {
            int result = a - b;
            System.Console.WriteLine("Subtraction result is {0}", result);
        }

        public static void Multiplication(int a, int b)
        {
            int result = a * b;
            System.Console.WriteLine("Multiplication result is {0}", result);
        }

        public static void Division(int a, int b)
        {
            int result = a / b;
            Console.WriteLine("Division result is {0}", result);
        }
    } // 这里添加一个大括号

    public static void Main(string[] args)
    { 
        Console.WriteLine("Enter first number ");
        int num1 = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter Second number ");
        int num2 = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter operation symbols +, -, *, /");
        string op= Console.ReadLine();

        if (op.Equals("+"))
        {
            Program.Addition(num1, num2);
        }
        else if (op.Equals("-"))
        {
            Program.Subtraction(num1, num2);
        }
        else if (op.Equals("*"))
        {
            Program.Multiplication(num1, num2);
        }
        else if (op.Equals("/"))
        {
            Program.Division(num1, num2);
        }
        else 
        {
            Console.WriteLine("Operation symbol is invalid");
        }

        Console.ReadLine();
    }
}

这个错误是因为意外地结束了 Program 类,而 Main 方法在类外部,这在 C# 编程语言中是不允许的。

英文:

You just need to move one curly brace between the Division and Main methods to the end of the file.
The code will look like this (I formatted it to look better):

using System;

namespace Calculator
{
    public class Program
    {
        public static void Addition(int a, int b)
        {
            int result = a + b;
            System.Console.WriteLine("Addition result is {0}", result);
        }

        public static void Subtraction(int a, int b)
        {
            int result = a - b;
            System.Console.WriteLine("Subtraction result is {0}", result);
        }

        public static void Multiplication(int a, int b)
        {
            int result = a * b;
            System.Console.WriteLine("Multiplication result is {0}", result);
        }

        public static void Division(int a, int b)
        {
            int result = a / b;
            Console.WriteLine("Division result is {0}", result);
        }

        public static void Main(string[] args)
        { 
            Console.WriteLine("Enter first number ");
            int num1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Second number ");
            int num2 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter operation symbols +, -, *, /");
            string op= Console.ReadLine();

            if (op.Equals("+"))
            {
                Program.Addition(num1, num2);
            }
            else if (op.Equals("-"))
            {
                Program.Subtraction(num1, num2);
            }
            else if (op.Equals("*"))
            {
                Program.Multiplication(num1, num2);
            }
            else if (op.Equals("/"))
            {
                Program.Division(num1, num2);
            }
            else 
            {
                Console.WriteLine("Operation symbol is invalid");
            }

            Console.ReadLine();
        }
    }
}

The error occurs because you accidentally end the Program class and the Main method is outside the class, which is not allowed in the C# programming language

huangapple
  • 本文由 发表于 2023年5月22日 01:32:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定