[英]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.