gpt4 book ai didi

c# - 如何摆脱编译器错误: C# use of unassigned local variable?

转载 作者:行者123 更新时间:2023-12-02 10:49:23 24 4
gpt4 key购买 nike

我有一些疑问,为什么C#不喜欢coordinates[j]部分,以及我该怎么做。

string[] lines = System.IO.File.ReadAllLines(@"C:\Users\sp\Dropbox\ProjectEuler\102\p102_triangles.txt");
string[] coordinates_str;
double[] coordinates; //Contains the coordinates for each line A1(x,y), A2(x,y), A3(x,y)
long ln = lines.Length;
Console.WriteLine("Length: " + ln.ToString());
for (int i = 0; i < ln; i++)
{
Console.Write(i);
Console.Write(lines[i]);
coordinates_str = lines[i].Split(',');
for (int j = 0; j < 6; j++)
{
coordinates[j] = Convert.ToDouble(coordinates_str[j]);
}
}

最佳答案

您可以为coordinates元素分配值,而无需先实际分配存储空间

double[] coordinates = new double[6];

到目前为止,您刚刚说过 coordinates是对 double 数组的引用,但是您还没有说该数组有多大(您尚未分配任何存储空间)。

关于c# - 如何摆脱编译器错误: C# use of unassigned local variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26415693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com