gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:15:47 27 4
gpt4 key购买 nike

作为我大学的一项强制性任务,要求我们使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com