c# - 为什么循环不起作用?为什么在局部变量 "y"总是 int 而 s counter = 1 = int

标签 c# parsing compiler-construction compiler-errors

作为我大学的一项强制性任务,要求我们使用 c# 我正在尝试构建一个包含 3 个阶段的编译器,这是我一生中第一次用 c# 编写,也是第一次在这样的大型项目中工作
1)解析器
2)错误检查器
3)内存
为什么循环不起作用?为什么在局部变量“y”总是 int 而 s counter = 1 = “identifier”

        foreach (string word in words)
            {
                string y = "";
                string z = "";
                string m = "";
                string er = "";
                string numb = "";
                string vari = "";
               List<string> s = new List<string>();


                if (word == "/")
                {
                    y += word;
                    y += "= operand\n";
                    s.Add(word);

                }
                else if (word == "+")
                {
                    y += word;
                    y += "= operand\n";
                    s.Add(word);

                }
                else if (word == "*")
                {
                    y += word;
                    y += "= operand\n";
                    s.Add(word);

最佳答案

用 for 替换你的 foreach 循环怎么样?
http://www.david-amador.com/2009/12/csharp-foreach-vs-for-loop/

您的循环将如下所示:

    for (int index = 0; index < words.Count; index++)
    {
        var word = words[index];
        //doStuff
    }

关于c# - 为什么循环不起作用?为什么在局部变量 "y"总是 int 而 s counter = 1 = int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34450425/

相关文章:

visual-c++ - 为什么VC++编译器MOV + PUSH args不仅仅是PUSH它们? x86

c++ - 编译器是否假定 "this"在 Debug模式下不是 nullptr?

c++ - 使用 flex/yacc 编写 C++ 编译器需要多少时间?

C# 自定义记录器 - 捕获发送对象的最佳方法

c# - "valuetype?"属性的 getter 和 setter 可以有不同的返回类型吗?

c# - 在 c# 中,当向方法发送参数时,我们应该什么时候使用 "ref"什么时候使用 "out"以及什么时候不使用它们?

parsing - Jsoup绝对路径

c# - 继承相关问题

java - 用 Java 解析 JSON 双括号

java - Java 有哪些 HTML 解析库推荐