gpt4 book ai didi

c# - 为什么我的数组脱离上下文?

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:02 26 4
gpt4 key购买 nike

我有以下程序:

bool boolFlag=true;
uint length;
Console.WriteLine("Give me the length of array a:");
if (boolFlag=UInt32.TryParse(Console.ReadLine(), out length))
{
int[] a = new int[length];
Console.WriteLine("Give me {0} int numbers for array a[] :", a.Length);
for (int i = 0; i < a.Length; i++)
{
if (boolFlag = Int32.TryParse(Console.ReadLine(), out a[i]))
{
continue;
}
else
{
Console.WriteLine("Parsing at Index a[{0}] failed.", i);
}
}
}
else
{
Console.WriteLine("Could not parse length.");
}


Console.WriteLine("Give me the length of array b:");
if (boolFlag = UInt32.TryParse(Console.ReadLine(), out length))
{
int[] b = new int[length];
Console.WriteLine("Give me {0} int numbers for array b[] :", b.Length);
for (int i = 0; i < b.Length; i++)
{
if (boolFlag = Int32.TryParse(Console.ReadLine(), out b[i]))
{
continue;
}
else
{
Console.WriteLine("Parsing at Index b[{0}] failed.", i);
}
}
}
else
{
Console.WriteLine("Could not parse length.");
}





if (a.Length==b.Length)
//{
// Console.WriteLine("a and b have equal Length");
// for (int i = 0; i < a.Length; i++)
// {
// if (a[i]==b[i])
// {
// boolFlag = true;
// continue;
// }
// else
// {
// Console.WriteLine("a[{0}] != b[{0}]", i);
// }
// }
//}
//else
//{
// Console.WriteLine("The arrays don`t have equal length");
//}
Console.ReadLine();

当我到达比较部分时,我得到:
错误 1 ​​The name 'a' does not exist in the current context
错误 2 The name 'b' does not exist in the current context

为什么会这样?

我第二次尝试更改代码...声明数组超出语句范围:

int[] a;
int[] b;
bool boolFlag=true;
uint length;
Console.WriteLine("Give me the length of array a:");
if (boolFlag=UInt32.TryParse(Console.ReadLine(), out length))
{
a = new int[length];
Console.WriteLine("Give me {0} int numbers for array a[] :", a.Length);
for (int i = 0; i < a.Length; i++)
{
if (boolFlag = Int32.TryParse(Console.ReadLine(), out a[i]))
{
continue;
}
else
{
Console.WriteLine("Parsing at Index a[{0}] failed.", i);
}
}
}
else
{
Console.WriteLine("Could not parse length.");
}


Console.WriteLine("Give me the length of array b:");
if (boolFlag = UInt32.TryParse(Console.ReadLine(), out length))
{
b = new int[length];
Console.WriteLine("Give me {0} int numbers for array b[] :", b.Length);
for (int i = 0; i < b.Length; i++)
{
if (boolFlag = Int32.TryParse(Console.ReadLine(), out b[i]))
{
continue;
}
else
{
Console.WriteLine("Parsing at Index b[{0}] failed.", i);
}
}
}
else
{
Console.WriteLine("Could not parse length.");
}





if (a.Length==b.Length)
{
// Console.WriteLine("a and b have equal Length");
// for (int i = 0; i < a.Length; i++)
// {
// if (a[i]==b[i])
// {
// boolFlag = true;
// continue;
// }
// else
// {
// Console.WriteLine("a[{0}] != b[{0}]", i);
// }
// }
}
else
{
// Console.WriteLine("The arrays don`t have equal length");
}


Console.ReadLine();

现在我得到:

错误 1 ​​使用未分配的局部变量 'b'
错误 2 使用未分配的局部变量 'a'

最佳答案

因为您在它们的范围之外使用 ab 。将它们的声明移动到代码的开头并将它们分配给 null。请注意,您可能需要考虑在用户输入错误长度时停止执行。在显示错误信息后放置return;

此外,请考虑查看 this关于变量作用域的有用文章。

关于c# - 为什么我的数组脱离上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30000982/

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