[英]Error CS0029: Cannot implicitly convert type 'string' to 'int'
在我的程序中,我必须取 7 个名字并按字母顺序显示它们。 但我得到了错误
错误 CS0029:无法将类型“string”隐式转换为“int”
我究竟做错了什么?
代码:
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 方法返回一个字符串。 所以这段代码尝试为 int 变量 people 分配一个字符串值。 重写如下代码。
String[] people = new String[size];
或者
var people = new String[size];
你应该定义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.