需要更改以使此代码簿门票按数字顺序而不是随机排列吗?
Changes needed to make this code book tickets in numerical order rather than random?
** 我希望代码按数字顺序打印出来,而不是像目前那样打印随机整数,因为我现在想填写从 1 到上限 32 的预订。在此先感谢您对此的任何帮助。
抱歉,如果我的英语不是最好的,那不是我的母语!**
class FirstClassCompartment
{
public void FirstClassTickets()
{
Random rand = new Random();
bool[] seats = new bool[32];
List<int> seatsBooked = new List<int>();
int completeBooking = 0;
bool quit = false;
string ticketAmount = "";
{
Console.Clear();
Console.WriteLine("\nWelcome to the First Class booking menu");
Console.WriteLine("");
Console.WriteLine("Please enter the amount of tickets you would like to book");
ticketAmount = Console.ReadLine();
}
do
{
Console.Clear();
Console.WriteLine("\nFirst Class booking menu");
Console.WriteLine("");
Console.WriteLine("Please press 1 to complete your first class booking");
completeBooking = Int32.Parse(Console.ReadLine());
switch (completeBooking)
{
case 1:
int firstClassSeat;
if (seatsBooked.Count == 0)
{
firstClassSeat = rand.Next(32);
seats[firstClassSeat] = true;
seatsBooked.Add(firstClassSeat);
}
else
{
do
{
firstClassSeat = rand.Next(32);
if (!seatsBooked.Contains(firstClassSeat))
{
seats[firstClassSeat] = true;
}
}
while (seatsBooked.Contains(firstClassSeat) && seatsBooked.Count < 32);
if (seatsBooked.Count < 32)
{
seatsBooked.Add(firstClassSeat);
}
}
if (seatsBooked.Count >= 32)
{
Console.WriteLine("All seats for first class have been booked");
Console.WriteLine("Please press enter to continue...");
}
else
{
Console.WriteLine("Your seat: {0}", firstClassSeat + 1);
Console.WriteLine("Please press enter to continue...");
}
Console.ReadLine();
break;
default:
Console.WriteLine("ERROR: INVALID SELECTION");
quit = true;
break;
}
} while (!quit);
}
}
}
强文本
将 int firstClassSeat=0
放在 do{} while() 循环之前,而不是放在 switch() 语句中。然后,而不是 firstClassSeat=rand.Next(32);
简单地做 firstClassSeat++;
** 我希望代码按数字顺序打印出来,而不是像目前那样打印随机整数,因为我现在想填写从 1 到上限 32 的预订。在此先感谢您对此的任何帮助。
抱歉,如果我的英语不是最好的,那不是我的母语!**
class FirstClassCompartment
{
public void FirstClassTickets()
{
Random rand = new Random();
bool[] seats = new bool[32];
List<int> seatsBooked = new List<int>();
int completeBooking = 0;
bool quit = false;
string ticketAmount = "";
{
Console.Clear();
Console.WriteLine("\nWelcome to the First Class booking menu");
Console.WriteLine("");
Console.WriteLine("Please enter the amount of tickets you would like to book");
ticketAmount = Console.ReadLine();
}
do
{
Console.Clear();
Console.WriteLine("\nFirst Class booking menu");
Console.WriteLine("");
Console.WriteLine("Please press 1 to complete your first class booking");
completeBooking = Int32.Parse(Console.ReadLine());
switch (completeBooking)
{
case 1:
int firstClassSeat;
if (seatsBooked.Count == 0)
{
firstClassSeat = rand.Next(32);
seats[firstClassSeat] = true;
seatsBooked.Add(firstClassSeat);
}
else
{
do
{
firstClassSeat = rand.Next(32);
if (!seatsBooked.Contains(firstClassSeat))
{
seats[firstClassSeat] = true;
}
}
while (seatsBooked.Contains(firstClassSeat) && seatsBooked.Count < 32);
if (seatsBooked.Count < 32)
{
seatsBooked.Add(firstClassSeat);
}
}
if (seatsBooked.Count >= 32)
{
Console.WriteLine("All seats for first class have been booked");
Console.WriteLine("Please press enter to continue...");
}
else
{
Console.WriteLine("Your seat: {0}", firstClassSeat + 1);
Console.WriteLine("Please press enter to continue...");
}
Console.ReadLine();
break;
default:
Console.WriteLine("ERROR: INVALID SELECTION");
quit = true;
break;
}
} while (!quit);
}
}
}
强文本
将 int firstClassSeat=0
放在 do{} while() 循环之前,而不是放在 switch() 语句中。然后,而不是 firstClassSeat=rand.Next(32);
简单地做 firstClassSeat++;