English 中文(简体)
当我称之为一种方法时,变数重新set为零
原标题:Variables reset to zero when I call a method

我想choice = 1 仅能被选定五倍,因此,我初步形成了一个可变的<代码>firstClass = 0,然后建立了一个可复制的firstClass < 5。 我在担任反职务时,列入<代码>第1Class++。 然而,我认为,每当我称之为<条形码>时,第一册正在重新启用。 我如何能够防止这种情况发生? 提前感谢。

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?
");
            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();
            }
        }
    }
}
最佳回答

这是完全正常的。 如果您希望这些变量在方法之外存在,那么你必须宣布<>......>在方法之外作为“领域”。 简单地转移:

int firstClass = 0;

在方法之外添加<代码>static modifier(在这种情况下):

static int firstClass = 0;

还要指出的是,这本身并不容易安全;如果翻新是一个问题(例如,ASP.NET),则使用<代码>int newValue = Interlock。 增量(参考第一卷);。 我只提到这一点,因为在一般情况下<>>>。 数据应当考虑校对,但我怀疑,在你的案件中,数据没有问题。

问题回答

第一组变量是方法。 每当这种方法被称作变数时,都会重新采用。 要想使首批集散射器成为一种持续的反射,就必须在班级内不使用这种方法。

You need to take any exit condition out of your method and put it outside, either by making a new method or putting it in the one that s calling it already.

例如,你可以做这样的事情:

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?
");
            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);
            }
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...