-2

下面是我正在处理的代码的一部分,执行会出现以下错误。

'不能将类型 void 隐式转换为字符串'。

我该如何解决?

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;
    }
}
4

3 回答 3

4

当我向你展示代码时

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 fiddle,我得到:

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

修复缺失nforeachto

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;
        }
    }
}

一个运行没有错误的程序。

在此旅程任何地方都没有弹出错误消息。

于 2018-08-09T04:56:32.703 回答
0

如果输出是返回类型为 void 的函数,则不能将其值分配给 strArray[i]。在每种情况下,确保函数的返回值不是 void。

此外,问题可能出在使用foreach中,因此请用 for循环替换它,因为更改foreach的枚举数可能会产生问题。

于 2018-08-09T03:05:20.070 回答
0

尝试删除 void 类型或 string[] argsinsideMain(string[] args)

也(不是另一个解决方案),但如果你必须提出改进

if(condtion==1){...}
if(condtion==2){...}
if(condtion==3){...}

ETC

更好的做法是将其替换为 Switch,这样您的程序就不会在每次调用您的方法时检查很多 ifs

于 2018-08-09T05:05:30.377 回答