为什么我的数组没有上下文?

只是

我有以下程序:

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名称'a'在当前上下文中不存在
错误2名称'b'在当前上下文中不存在

为什么会发生这种情况?

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

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'

yazanpro

因为您正在使用a并且b不在其范围内。将它们的声明移到代码的开头,然后将其分配给null请注意,当用户输入错误的长度时,您可能要考虑停止执行。return;显示错误消息后放置

此外,考虑检查出这个关于变量的作用域有用的文章。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我的数组没有上下文?

来自分类Dev

没有正确的上下文......为什么?

来自分类Dev

为什么我的Vue方法没有引用正确的上下文(数据)?

来自分类Dev

为什么我没有虚拟打印机的上下文?

来自分类Dev

为什么我在tcl中没有上下文用户错误?

来自分类Dev

反应:为什么我的上下文值没有更新?

来自分类Dev

反应:为什么我的上下文值没有更新?

来自分类Dev

为什么我的 Ember 组件中的 .then() 中没有“this”上下文

来自分类Dev

为什么我的 WebGL 上下文只有一部分扩展

来自分类Dev

为什么我的主要上下文遍历标题?

来自分类Dev

为什么我的数据库上下文被处置

来自分类Dev

为什么在有或没有上下文的情况下捕获异常关闭打开的文件?

来自分类Dev

在上下文菜单中选择“在终端中打开”时,为什么没有源.profile?

来自分类Dev

为什么LayoutInflater需要上下文?

来自分类Dev

为什么上下文=无?

来自分类Dev

上下文评估有什么功能

来自分类Dev

为什么我不能在jboss的根上下文中设置我的应用程序?

来自分类Dev

为什么即使我有#[tokio :: main],也会收到错误消息“没有反应堆在运行,必须从Tokio运行时的上下文中调用”?

来自分类Dev

为什么在静态上下文中进行数组初始化?

来自分类Dev

为什么我需要一个ToList()来避免处理上下文错误?

来自分类Dev

为什么我无法使用GLFW获得向前兼容的OpenGL上下文?

来自分类Dev

为什么我的JSF应用程序不显示上下文消息?

来自分类Dev

为什么驱动程序内存不在我的Spark上下文配置中?

来自分类Dev

为什么在Cocos2D中我的32位上下文周围出现黑线?

来自分类Dev

为什么我的webGL上下文返回为null,同时还显示已检索到它?

来自分类Dev

为什么我的滑行上下文返回错误在logcat中为空?

来自分类Dev

为什么我的程序无法创建新的 JAAS 登录上下文?

来自分类Dev

为什么我需要 Spring Boot Restful api 的上下文根?

来自分类Dev

为什么在我的行上下文中使用 Early 函数时不起作用

Related 相关文章

  1. 1

    为什么我的数组没有上下文?

  2. 2

    没有正确的上下文......为什么?

  3. 3

    为什么我的Vue方法没有引用正确的上下文(数据)?

  4. 4

    为什么我没有虚拟打印机的上下文?

  5. 5

    为什么我在tcl中没有上下文用户错误?

  6. 6

    反应:为什么我的上下文值没有更新?

  7. 7

    反应:为什么我的上下文值没有更新?

  8. 8

    为什么我的 Ember 组件中的 .then() 中没有“this”上下文

  9. 9

    为什么我的 WebGL 上下文只有一部分扩展

  10. 10

    为什么我的主要上下文遍历标题?

  11. 11

    为什么我的数据库上下文被处置

  12. 12

    为什么在有或没有上下文的情况下捕获异常关闭打开的文件?

  13. 13

    在上下文菜单中选择“在终端中打开”时,为什么没有源.profile?

  14. 14

    为什么LayoutInflater需要上下文?

  15. 15

    为什么上下文=无?

  16. 16

    上下文评估有什么功能

  17. 17

    为什么我不能在jboss的根上下文中设置我的应用程序?

  18. 18

    为什么即使我有#[tokio :: main],也会收到错误消息“没有反应堆在运行,必须从Tokio运行时的上下文中调用”?

  19. 19

    为什么在静态上下文中进行数组初始化?

  20. 20

    为什么我需要一个ToList()来避免处理上下文错误?

  21. 21

    为什么我无法使用GLFW获得向前兼容的OpenGL上下文?

  22. 22

    为什么我的JSF应用程序不显示上下文消息?

  23. 23

    为什么驱动程序内存不在我的Spark上下文配置中?

  24. 24

    为什么在Cocos2D中我的32位上下文周围出现黑线?

  25. 25

    为什么我的webGL上下文返回为null,同时还显示已检索到它?

  26. 26

    为什么我的滑行上下文返回错误在logcat中为空?

  27. 27

    为什么我的程序无法创建新的 JAAS 登录上下文?

  28. 28

    为什么我需要 Spring Boot Restful api 的上下文根?

  29. 29

    为什么在我的行上下文中使用 Early 函数时不起作用

热门标签

归档