当我调用方法时,变量重置为零

时间:2011-10-12 18:07:14

标签: c# console-application

我希望choice == 1只能被选中五次,所以我初始化了一个变量firstClass = 0,然后为firstClass < 5设置了一个do-while。我把firstClass++包括在我的行动中作为一个反击。但是,我认为每次调用方法CheckIn()时,firstClass都会重新初始化。我怎样才能防止这种情况发生?提前谢谢。

using System;

namespace Assignment7
{
    class Plane
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the Airline Reservation System.");
            Console.WriteLine("Where would you like to sit?\n");
            Console.WriteLine("Enter 1 for First Class.");
            Console.WriteLine("Enter 2 for Economy.");
            CheckIn();
        }

        public static void CheckIn()
        {
            int choice = Convert.ToInt32(Console.ReadLine());
            int firstClass = 0;
            int economy = 0;

            if (choice == 1)
            {
                do
                {
                    Console.WriteLine("You have chosen a First Class seat.");
                    firstClass++;
                    CheckIn();
                } while (firstClass < 5);
            }
            else if (choice == 2)
            {
                do
                {
                    Console.WriteLine("You have chosen an Economy seat.");
                    economy++;
                    CheckIn();
                } while (economy < 5);
            }
            else
            {
                Console.WriteLine("That does not compute.");
                CheckIn();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

这完全正常。如果希望变量存在于方法之外,则必须在方法之外声明它作为“字段”。只需移动:

int firstClass = 0;

在方法之外,添加static修饰符(在本例中):

static int firstClass = 0;

另请注意,这本身并不是线程安全的;如果线程是一个问题(例如,ASP.NET),那么使用int newValue = Interlocked.Increment(ref firstClass);。我只提到这一点,因为在一般情况下 static数据应该考虑线程,但我怀疑它不是你的情况(控制台exe)的问题。

答案 1 :(得分:1)

firstClass变量是方法范围。每次调用该方法时,都会重新初始化该变量。要让firstClass成为一个持续的计数器,它需要在类中的方法之外。

答案 2 :(得分:0)

您需要从您的方法中取出任何退出条件并将其放在外面,方法是创建一个新方法或将其放入已经调用它的方法中。

例如,您可以执行以下操作:

using System;

namespace Assignment7
{
    class Plane
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the Airline Reservation System.");
            Console.WriteLine("Where would you like to sit?\n");
            Console.WriteLine("Enter 1 for First Class.");
            Console.WriteLine("Enter 2 for Economy.");
            CheckIn(0, 0);
        }

        public static void CheckIn(int firstClassSeatsTaken, int economySeatsTaken)
        {
            int choice = Convert.ToInt32(Console.ReadLine());

            if (choice == 1)
            {
                do
                {
                    Console.WriteLine("You have chosen a First Class seat.");
                    firstClass++;
                    CheckIn(firstClassSeatsTaken, economySeatsTaken);
                } while (firstClass < 5);
            }
            else if (choice == 2)
            {
                do
                {
                    Console.WriteLine("You have chosen an Economy seat.");
                    economy++;
                    CheckIn(firstClassSeatsTaken, economySeatsTaken);
                } while (economy < 5);
            }
            else
            {
                Console.WriteLine("That does not compute.");
                CheckIn(firstClassSeatsTaken, economySeatsTaken);
            }
        }
    }
}