英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论