3 回答
TA贡献1995条经验 获得超2个赞
当我向你展示代码时
private static void Main(string[] args)
{
string[] strArray = new string[n];
int i = 0;
foreach (example)
{
if (condition 1)
{
strArray[i] = output;
}
if (condition 2)
{
strArray[i] = output;
}
if (condition 3)
{
strArray[i] = output;
}
i = i + 1;
}
}
进入 .net 小提琴,我得到:
Compilation error (line 1, col 16): Expected class, delegate, enum, interface, or struct
Compilation error (line 1, col 33): Identifier expected
Compilation error (line 1, col 35): Expected class, delegate, enum, interface, or struct
Compilation error (line 3, col 12): Identifier expected
Compilation error (line 3, col 14): Expected class, delegate, enum, interface, or struct
Compilation error (line 3, col 29): Expected class, delegate, enum, interface, or struct
Compilation error (line 3, col 38): Expected class, delegate, enum, interface, or struct
Compilation error (line 10, col 25): Expected class, delegate, enum, interface, or struct
Compilation error (line 11, col 9): Type or namespace definition, or end-of-file expected
围绕它
public class MyClass
{
// your code
}
留给我
Compilation error (line 8, col 21): Identifier expected
Compilation error (line 8, col 22): ) expected
Compilation error (line 10, col 23): ) expected
Compilation error (line 10, col 24): ; expected
Compilation error (line 10, col 24): Invalid expression term ')'
Compilation error (line 10, col 25): ; expected
Compilation error (line 14, col 23): ) expected
Compilation error (line 14, col 24): ; expected
Compilation error (line 14, col 24): Invalid expression term ')'
Compilation error (line 14, col 25): ; expected
Compilation error (line 18, col 23): ) expected
Compilation error (line 18, col 24): ; expected
Compilation error (line 18, col 24): Invalid expression term ')'
Compilation error (line 18, col 25): ; expected
修复丢失的n和foreach到
string[] strArray = new string[10];
int i = 0;
var data = new [] {"1","2","3","4"};
foreach (var example in data)
{
// your inner code
}
留给我
Compilation error (line 11, col 23): ) expected
Compilation error (line 11, col 24): ; expected
Compilation error (line 11, col 24): Invalid expression term ')'
Compilation error (line 11, col 25): ; expected
Compilation error (line 15, col 23): ) expected
Compilation error (line 15, col 24): ; expected
Compilation error (line 15, col 24): Invalid expression term ')'
Compilation error (line 15, col 25): ; expected
Compilation error (line 19, col 23): ) expected
Compilation error (line 19, col 24): ; expected
Compilation error (line 19, col 24): Invalid expression term ')'
Compilation error (line 19, col 25): ; expected
修复内部条件
if (i==1)
{
strArray[i] = "There is";
}
if (i==2)
{
strArray[i] = "is no";
}
if (i==3)
{
strArray[i] = "no spoon.";
}
i = i + 1;
导致缺少公共静态 Main,修复导致
public class MyClass
{
public static void Main(string[] args)
{
string[] strArray = new string[10];
int i = 0;
var data = new[]{"1", "2", "3", "4"};
foreach (var example in data)
{
if (i == 1)
{
strArray[i] = "There is";
}
if (i == 2)
{
strArray[i] = "is no";
}
if (i == 3)
{
strArray[i] = "no spoon.";
}
i = i + 1;
}
}
}
一个运行没有错误的程序。
在此过程中没有任何地方弹出您的错误消息。
TA贡献1868条经验 获得超4个赞
如果输出是一个返回类型为 void 的函数,则不能将其值赋给 strArray[i]。在每种情况下,确保函数的返回值不为空。
此外,问题可能在于使用foreach,因此将其替换为 for循环,因为更改foreach的枚举器可能会产生问题。
TA贡献1877条经验 获得超6个赞
尝试删除 void 类型或 string[] argsinsideMain(string[] args)
也(不是另一种解决方案)但是如果你必须把它改进
if(condtion==1){...}
if(condtion==2){...}
if(condtion==3){...}
等等
更好的做法是将其替换为 Switch,这样您的程序就不会在每次调用方法时检查大量 if
- 3 回答
- 0 关注
- 180 浏览
添加回答
举报