简体   繁体   English

错误 CS0029:无法将类型“string”隐式转换为“int”

[英]Error CS0029: Cannot implicitly convert type 'string' to 'int'

In my program, I have to take 7 names and display them in alphabetical order.在我的程序中,我必须取 7 个名字并按字母顺序显示它们。 But I get the error但我得到了错误

Error CS0029: Cannot implicitly convert type 'string' to 'int'错误 CS0029:无法将类型“string”隐式转换为“int”

What am I doing wrong?我究竟做错了什么?

Code:代码:

static void Main(string[] args)
{
    int size = 7;
    
    int[] people = new int[size];
    
    for(int i = 0; i < size; i++)
    {
        Console.WriteLine("Please enter " + i+1 + ". person's name: ");
    
        // Here
        people[i] = Console.ReadLine();
    }
    
    Console.WriteLine("After alphabetic ordering: ");
    
    Array.Sort(people);
    
    for (int j = 0; j < size; j++)
    {
        Console.WriteLine(j + 1 + "person's name : " + people[j]);
    }

    // ...
}

ReadLine method returns a string. ReadLine 方法返回一个字符串。 So this code try to assign a string value to the int variable people.所以这段代码尝试为 int 变量 people 分配一个字符串值。 Rewrite the code like below.重写如下代码。

String[] people = new String[size];

or或者

var people = new String[size];

You sould define string[] Not int[] so Use string[]你应该定义string[]不是int[]所以使用 string[]

static void Main(string[] args)
{
    int size = 7;
    
    string[] people = new string[size];
    
    for(int i = 0; i < size; i++)
    {
        Console.WriteLine("Please enter " + i+1 + ". person's name: ");
        
        // Here
        people[i] = Console.ReadLine();
    }
    
    Console.WriteLine("After alphabetic ordering: ");
    
    
    
    Array.Sort(people);
    
    for (int j = 0; j < size; j++)
    {
        Console.WriteLine(j + 1 + "person's name : " + people[j]);
    }

    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 CS0029:无法将类型“int”隐式转换为“string” - Error CS0029: Cannot implicitly convert type 'int' to 'string' 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;Score&#39; - error CS0029: Cannot implicitly convert type 'int' to 'Score' 错误 CS0029 无法将类型“字符串”隐式转换为“双精度” - Error CS0029 Cannot implicitly convert type 'string' to 'double' 错误CS0029无法将类型“字符串”隐式转换为“十进制” - Error CS0029 Cannot implicitly convert type 'string' to 'decimal' 编译器错误消息:CS0029:无法将类型“int”隐式转换为“string”错误 - Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' error 编译器错误消息:CS0029:无法将类型“int”隐式转换为“string” - Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39; - CS0029: Cannot implicitly convert type 'int' to 'bool' CS0029 C#无法将类型隐式转换为&#39;string []&#39; - CS0029 C# Cannot implicitly convert type to 'string[]' 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;bool&#39;-Unity3D - error CS0029: Cannot implicitly convert type `int' to `bool' - Unity3D
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM