我想要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();
}
}
}
}