我想要choice == 1
只能被选中五次,所以我初始化了一个变量firstClass = 0
,然后为firstClass < 5
设置了一个do-while。我将firstClass++
包括在我的do-while中作为柜台。不过,我认为的Firstclass重新初始化每次我调用该方法CheckIn()
时间。我怎样才能防止这种情况发生?提前致谢。当我调用方法时,变量重置为零
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();
}
}
}
}