无法执行 c# 程序不包含静态主方法错误

本文关键字:静态 方法 错误 包含 执行 程序 | 更新日期: 2023-09-27 18:36:25

>我正在尝试执行下面的程序,但它显示错误"程序不包含适合入口点 CSC 的静态主方法"......尝试使用堆栈(BODMAS)计算算术表达式。如果有人知道,请解决这个问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class Class1
    {
        public static string evaluate1(String expression)
        {
            char[] charArr = expression.ToCharArray();

            // Stack for numbers: 'values'
            Stack<Int32> values = new Stack<Int32>();
            // Stack for Operators: 'ops'
            Stack<Char> ops = new Stack<Char>();
            for (int i = 0; i < charArr.Length; i++)
            {
                // Current token is a whitespace, skip it
                if (charArr[i] == ' ')
                    continue;
                // Current token is a number, push it to stack for numbers
                if (charArr[i] >= '0' && charArr[i] <= '9')
                {
                    StringBuilder sbuf = new StringBuilder();
                    //StringBuffer sbuf = new StringBuffer();
                    // There may be more than one digits in number
                    while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                        sbuf.Append(charArr[i++]);
                    values.Push(Convert.ToInt32(sbuf.ToString()));
                }
                // Current token is an opening brace, push it to 'ops'
                else if (charArr[i] == '(')
                    ops.Push(charArr[i]);
                // Closing brace encountered, solve entire brace
                else if (charArr[i] == ')')
                {
                    while (ops.Peek() != '(')
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    ops.Pop();
                }
                // Current token is an operator.
                else if (charArr[i] == '+' || charArr[i] == '-' ||
                         charArr[i] == '*' || charArr[i] == '/')
                {
                    // While top of 'ops' has same or greater precedence to current
                    // token, which is an operator. Apply operator on top of 'ops'
                    // to top two elements in values stack
                    while (hasPrecedence(charArr[i], ops.Peek()))
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    // Push current token to 'ops'.
                    ops.Push(charArr[i]);
                }
            }
            // Entire expression has been parsed at this point, apply remaining
            // ops to remaining values
            while (!ops.Equals(0))
                values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

            // Top of 'values' contains result, return it
            return values.Pop().ToString();
        }
        // Returns true if 'op2' has higher or same precedence as 'op1',
        // otherwise returns false.
        public static bool hasPrecedence(char op1, char op2)
        {
            if (op2 == '(' || op2 == ')')
                return false;
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            else
                return true;
        }
        // A utility method to apply an operator 'op' on operands 'a' 
        // and 'b'. Return the result.
        public static int applyOp(char op, int b, int a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;
            }
            return 0;
        }
        public static void main(String[] args)
        {
            Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
        }
    }
}

我编辑如下。

namespace ConsoleApplication1
{
    public class Class1
    {
        public static string evaluate1(String expression)
        {
            char[] charArr = expression.ToCharArray();

            // Stack for numbers: 'values'
            Stack<Int32> values = new Stack<Int32>();
            // Stack for Operators: 'ops'
            Stack<Char> ops = new Stack<Char>();
            for (int i = 0; i < charArr.Length; i++)
            {
                // Current token is a whitespace, skip it
                if (charArr[i] == ' ')
                    continue;
                // Current token is a number, push it to stack for numbers
                if (charArr[i] >= '0' && charArr[i] <= '9')
                {
                    StringBuilder sbuf = new StringBuilder();
                    //StringBuffer sbuf = new StringBuffer();
                    // There may be more than one digits in number
                    while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                        sbuf.Append(charArr[i++]);
                    values.Push(Convert.ToInt32(sbuf.ToString()));
                }
                // Current token is an opening brace, push it to 'ops'
                else if (charArr[i] == '(')
                    ops.Push(charArr[i]);
                // Closing brace encountered, solve entire brace
                else if (charArr[i] == ')')
                {
                    while (ops.Peek() != '(')
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    ops.Pop();
                }
                // Current token is an operator.
                else if (charArr[i] == '+' || charArr[i] == '-' ||
                         charArr[i] == '*' || charArr[i] == '/')
                {
                    // While top of 'ops' has same or greater precedence to current
                    // token, which is an operator. Apply operator on top of 'ops'
                    // to top two elements in values stack
                    while (hasPrecedence(charArr[i], ops.Peek()))
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    // Push current token to 'ops'.
                    ops.Push(charArr[i]);
                }
            }
            // Entire expression has been parsed at this point, apply remaining
            // ops to remaining values
            while (!ops.Equals(0))
                values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

            // Top of 'values' contains result, return it
            return values.Pop().ToString();
        }
        // Returns true if 'op2' has higher or same precedence as 'op1',
        // otherwise returns false.
        public static bool hasPrecedence(char op1, char op2)
        {
            if (op2 == '(' || op2 == ')')
                return false;
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            else
                return true;
        }
        // A utility method to apply an operator 'op' on operands 'a' 
        // and 'b'. Return the result.
        public static int applyOp(char op, int b, int a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;
            }
            return 0;
        }
    }
      public static void main(String[] args)
        {  
        Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
        }
}
namespace ConsoleApplication1
{
    public class Class1
    {
        public static string evaluate1(String expression)
        {
            char[] charArr = expression.ToCharArray();

            // Stack for numbers: 'values'
            Stack<Int32> values = new Stack<Int32>();
            // Stack for Operators: 'ops'
            Stack<Char> ops = new Stack<Char>();
            for (int i = 0; i < charArr.Length; i++)
            {
                // Current token is a whitespace, skip it
                if (charArr[i] == ' ')
                    continue;
                // Current token is a number, push it to stack for numbers
                if (charArr[i] >= '0' && charArr[i] <= '9')
                {
                    StringBuilder sbuf = new StringBuilder();
                    //StringBuffer sbuf = new StringBuffer();
                    // There may be more than one digits in number
                    while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                        sbuf.Append(charArr[i++]);
                    values.Push(Convert.ToInt32(sbuf.ToString()));
                }
                // Current token is an opening brace, push it to 'ops'
                else if (charArr[i] == '(')
                    ops.Push(charArr[i]);
                // Closing brace encountered, solve entire brace
                else if (charArr[i] == ')')
                {
                    while (ops.Peek() != '(')
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    ops.Pop();
                }
                // Current token is an operator.
                else if (charArr[i] == '+' || charArr[i] == '-' ||
                         charArr[i] == '*' || charArr[i] == '/')
                {
                    // While top of 'ops' has same or greater precedence to current
                    // token, which is an operator. Apply operator on top of 'ops'
                    // to top two elements in values stack
                    while (hasPrecedence(charArr[i], ops.Peek()))
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    // Push current token to 'ops'.
                    ops.Push(charArr[i]);
                }
            }
            // Entire expression has been parsed at this point, apply remaining
            // ops to remaining values
            while (!ops.Equals(0))
                values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

            // Top of 'values' contains result, return it
            return values.Pop().ToString();
        }
        // Returns true if 'op2' has higher or same precedence as 'op1',
        // otherwise returns false.
        public static bool hasPrecedence(char op1, char op2)
        {
            if (op2 == '(' || op2 == ')')
                return false;
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            else
                return true;
        }
        // A utility method to apply an operator 'op' on operands 'a' 
        // and 'b'. Return the result.
        public static int applyOp(char op, int b, int a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;
            }
            return 0;
        }
    }
      public static void main(String[] args)
        {  
        Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
        }
}
namespace ConsoleApplication1
{
    public class Class1
    {
        public static string evaluate1(String expression)
        {
            char[] charArr = expression.ToCharArray();

            // Stack for numbers: 'values'
            Stack<Int32> values = new Stack<Int32>();
            // Stack for Operators: 'ops'
            Stack<Char> ops = new Stack<Char>();
            for (int i = 0; i < charArr.Length; i++)
            {
                // Current token is a whitespace, skip it
                if (charArr[i] == ' ')
                    continue;
                // Current token is a number, push it to stack for numbers
                if (charArr[i] >= '0' && charArr[i] <= '9')
                {
                    StringBuilder sbuf = new StringBuilder();
                    //StringBuffer sbuf = new StringBuffer();
                    // There may be more than one digits in number
                    while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                        sbuf.Append(charArr[i++]);
                    values.Push(Convert.ToInt32(sbuf.ToString()));
                }
                // Current token is an opening brace, push it to 'ops'
                else if (charArr[i] == '(')
                    ops.Push(charArr[i]);
                // Closing brace encountered, solve entire brace
                else if (charArr[i] == ')')
                {
                    while (ops.Peek() != '(')
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    ops.Pop();
                }
                // Current token is an operator.
                else if (charArr[i] == '+' || charArr[i] == '-' ||
                         charArr[i] == '*' || charArr[i] == '/')
                {
                    // While top of 'ops' has same or greater precedence to current
                    // token, which is an operator. Apply operator on top of 'ops'
                    // to top two elements in values stack
                    while (hasPrecedence(charArr[i], ops.Peek()))
                        values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                    // Push current token to 'ops'.
                    ops.Push(charArr[i]);
                }
            }
            // Entire expression has been parsed at this point, apply remaining
            // ops to remaining values
            while (!ops.Equals(0))
                values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

            // Top of 'values' contains result, return it
            return values.Pop().ToString();
        }
        // Returns true if 'op2' has higher or same precedence as 'op1',
        // otherwise returns false.
        public static bool hasPrecedence(char op1, char op2)
        {
            if (op2 == '(' || op2 == ')')
                return false;
            if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                return false;
            else
                return true;
        }
        // A utility method to apply an operator 'op' on operands 'a' 
        // and 'b'. Return the result.
        public static int applyOp(char op, int b, int a)
        {
            switch (op)
            {
                case '+':
                    return a + b;
                case '-':
                    return a - b;
                case '*':
                    return a * b;
            }
            return 0;
        }
    }
      public static void main(String[] args)
        {  
        Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
        }
}
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1
    {
        public class Class1
        {
            public static string evaluate1(String expression)
            {
                char[] charArr = expression.ToCharArray();

                // Stack for numbers: 'values'
                Stack<Int32> values = new Stack<Int32>();
                // Stack for Operators: 'ops'
                Stack<Char> ops = new Stack<Char>();
                for (int i = 0; i < charArr.Length; i++)
                {
                    // Current token is a whitespace, skip it
                    if (charArr[i] == ' ')
                        continue;
                    // Current token is a number, push it to stack for numbers
                    if (charArr[i] >= '0' && charArr[i] <= '9')
                    {
                        StringBuilder sbuf = new StringBuilder();
                        //StringBuffer sbuf = new StringBuffer();
                        // There may be more than one digits in number
                        while (i < charArr.Length && charArr[i] >= '0' && charArr[i] <= '9')
                            sbuf.Append(charArr[i++]);
                        values.Push(Convert.ToInt32(sbuf.ToString()));
                    }
                    // Current token is an opening brace, push it to 'ops'
                    else if (charArr[i] == '(')
                        ops.Push(charArr[i]);
                    // Closing brace encountered, solve entire brace
                    else if (charArr[i] == ')')
                    {
                        while (ops.Peek() != '(')
                            values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                        ops.Pop();
                    }
                    // Current token is an operator.
                    else if (charArr[i] == '+' || charArr[i] == '-' ||
                             charArr[i] == '*' || charArr[i] == '/')
                    {
                        // While top of 'ops' has same or greater precedence to current
                        // token, which is an operator. Apply operator on top of 'ops'
                        // to top two elements in values stack
                        while (hasPrecedence(charArr[i], ops.Peek()))
                            values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));
                        // Push current token to 'ops'.
                        ops.Push(charArr[i]);
                    }
                }
                // Entire expression has been parsed at this point, apply remaining
                // ops to remaining values
                while (!ops.Equals(0))
                    values.Push(applyOp(ops.Pop(), values.Pop(), values.Pop()));

                // Top of 'values' contains result, return it
                return values.Pop().ToString();
            }
            // Returns true if 'op2' has higher or same precedence as 'op1',
            // otherwise returns false.
            public static bool hasPrecedence(char op1, char op2)
            {
                if (op2 == '(' || op2 == ')')
                    return false;
                if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
                    return false;
                else
                    return true;
            }
            // A utility method to apply an operator 'op' on operands 'a' 
            // and 'b'. Return the result.
            public static int applyOp(char op, int b, int a)
            {
                switch (op)
                {
                    case '+':
                        return a + b;
                    case '-':
                        return a - b;
                    case '*':
                        return a * b;
                }
                return 0;
            }
        }
          public static void main(String[] args)
            {  
            Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
            }
    }

我按上面编辑。现在接近无效预期类数代表错误即将到来

无法执行 c# 程序不包含静态主方法错误

那是

因为你的代码中有拼写错误。

main应该Main被认为是Visual studioMain方法。

对我来说

,您的初始代码似乎还可以,但是您只需要创建一个新类并从Class1中提取以下main方法并添加到新创建的类中

public class MainClass
{
    public static void main(String[] args)
    {
        Console.WriteLine(Class1.evaluate1("10 + 2 * 6"));
    }
}

注意:在您的编辑中,我认为您已经多次粘贴了代码。