找到你要的答案

Q:Why are my arrays out of context?

Q:为什么我的数组的上下文?

I have the following program :

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();

When i get to the comparison part i get :
Error 1 The name 'a' does not exist in the current context
Error 2 The name 'b' does not exist in the current context

Why does this happen ?

My 2nd try changed code ...declare arrays out of statements scope :

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();

Now i get :

Error 1 Use of unassigned local variable 'b'
Error 2 Use of unassigned local variable 'a'

我有以下程序:

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();

When i get to the comparison part i get :
Error 1 The name 'a' does not exist in the current context
Error 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();

现在我得到:

Error 1 Use of unassigned local variable 'b'
Error 2 Use of unassigned local variable 'a'

answer1: 回答1:

Because you are using a and b outside of their scope. Move their declaration to the beginning of your code and assign them to null. Note that you might want to consider stopping the execution when the user inputs bad length. Place return; after displaying the error message.

Also, consider checking out this useful article about variable scope.

因为你正在使用A和B以外的范围。将它们的声明移动到代码的开头并将它们分配给null。注意,当用户输入坏长度时,可能会考虑停止执行。放置返回;在显示错误消息后。

同时,考虑检查关于变量范围的有用的文章。

answer2: 回答2:

You need to declare the a and b arrays outside the ifs, even if you assign values to them inside them. So move int[] a=new int[0]; and int[]b = new int[0]; at the beginning of your code and change your if to if (a.length > 0 && b.length > 0 && a.length == b.length).

你需要申报的A和B的阵列在IFS,即使你在他们里面分配值。所以把int [] = new int,int [ 0 ];[ ] B =新国际[ 0 ];在你的代码的开始和改变你的如果如果(a.length >;0 &;&;b.length >;0 &;&;a.length = = b.length)。

c#  arrays