首页 > 解决方案 > 如何从 C# 中的循环外部访问循环内的变量?

问题描述

我尝试将变量放入循环内部和外部循环,以便其他函数可以使用该变量但仍然会出错。使用菜单 1 我创建了一个列表。然后我希望能够通过菜单 4 打印列表和平均值。我必须在菜单 1 上完成所有操作,只是为了看看它是否有效。但需要将其分解。1) 列表和 4) 显示。

bool exit = false;

        do
        {
            Console.WriteLine("");
            Console.WriteLine("1. Create age list");
            Console.WriteLine("2. Add age to list");
            Console.WriteLine("3. Remove age from list");
            Console.WriteLine("4. Display ages and average");
            Console.WriteLine("5. exit");
            Console.WriteLine("");
            string input = Console.ReadLine();
            Console.WriteLine("");

            List<int> ages = new List<int>();
            int totalAges = 0;
            int age;

            if (input == "1")
            {

                //List<int> ages = new List<int>();
                //int totalAges = 0;

                //User Input
                Console.WriteLine("How many ages do you want to enter? ");
                Console.WriteLine("");

                //While loop for TryParse
                while (!int.TryParse(Console.ReadLine(), out totalAges))
                {
                    Console.WriteLine("Please enter a valid number");
                    Console.WriteLine("");
                }
                while (ages.Count != totalAges)
                {
                    // Read grade
                    System.Console.WriteLine("Enter Grade: ");
                    //int age;

                    while (!int.TryParse(System.Console.ReadLine(), out age))
                    {
                        System.Console.WriteLine("Please enter a valid age.");
                    }

                    ages.Add(age);
                }

                for (int index = 0; index < ages.Count(); index++)
                {
                    Console.WriteLine("");
                    Console.WriteLine($"Age { ages[index] }");
                    Console.WriteLine("");
                    totalAges = ages.Sum();
                }

                double avg = 0.0;
                avg = totalAges / (ages.Count);
                Console.WriteLine("");
                Console.WriteLine($"The average age was { avg}");


            }
            else if (input == "2")
            {
                exit = true;
            }
            else if (input == "3")
            {
                exit = true;
            }
            else if (input == "4")
            {

                for (int index = 0; index < ages.Count(); index++)
                {
                    Console.WriteLine("");
                    Console.WriteLine($"Age { ages[index] }");
                    Console.WriteLine("");
                    totalAges = ages.Sum();
                }

                double avg = 0.0;
                avg = totalAges / (ages.Count);
                Console.WriteLine("");
                Console.WriteLine($"The average age was { avg}");
            }
            else if (input == "5")
            {
                exit = true;
            }

            else
            {
                Console.WriteLine("How hard is it to type 1 or 2? Try again!");
            }
        } while (exit == false);

标签: c#loopsvariablesif-statement

解决方案


我认为在这种情况下,不要在方法中而是在类中声明您尝试访问的变量会更有意义。如果您希望每次调用该方法时基本上都将其重置,则可以在该方法中每次实例化它。我还要做的是将 if 语句分解为它们自己的方法。这将允许您拥有更多 OOP 结构化代码。


推荐阅读