string[,] Arr = new string[4, 5]{{"1A" , " 2A" , " 3A" , " 4A" , " 5A"},
{"1B" , " 2B" , " 3B" , " 4B" , " 5B"},
{"1C" , " 2C" , " 3C" , " 4C" , " 5C"},
{"1D" , " 2D" , " 3D" , " 4D" , " 5D"}};
Console.WriteLine("List of availabe seats.");
Console.WriteLine();
for (int j = 0; j < Arr.Length; j++)
{
for (int i = 0; i < Arr.GetLength(0); i++)
{
for (int k = 0; k < Arr.GetLength(1); k++)
{
Console.Write(Arr[i, k]);
}
Console.WriteLine();
}
Console.WriteLine("Enter your seat: ");
Console.WriteLine("---------------- ");
string textToReplace = Console.ReadLine().ToLowerInvariant();
bool isFullyBooked = true;
bool isSeatTaken = true;
for (int row = Arr.GetLowerBound(0); row <= Arr.GetUpperBound(0); ++row)
{
for (int column = Arr.GetLowerBound(1); column <= Arr.GetUpperBound(1); ++column)
{
if (Arr[row, column].ToLowerInvariant().Contains(textToReplace))
{
Arr[row, column] = " X ";
isSeatTaken = false;
}
if (!Arr[row, column].Contains(" X "))
{
isFullyBooked = false;
}
}
}
if (isFullyBooked)
{
Console.WriteLine("Fully Booked");
Console.WriteLine("----------------");
}
if (isSeatTaken)
{
Console.WriteLine("Already Taken");
Console.WriteLine("----------------");
}
}
如何在程序满时关闭程序?如果我运行这个程序它只结束了20个循环,所以如果我输入两个“2A”,程序将无法完成所有席位,程序将关闭。我是c#的初学者。
答案 0 :(得分:0)
好吧,如果你需要&#34;快速和肮脏&#34;解决方案然后将您的fullyBooked
变量声明移到main for循环之外,并将for循环从for (int j = 0; j < Arr.Length; j++)
更改为类似
bool isFullyBooked = false;
while (!isFullyBooked)
{
// rest of your code, but change bool isFullyBooked = true;
// to simply isFullyBooked = true; without declaration, only assignment
// because it was declared earlier
因此,您的主循环将一直运行,直到所有座位都被预订。
但又一次 - 它非常快速而且肮脏&#34;修复,考虑重构您的实现,因为它看起来不是很好。 (我想这里有点偏离主题,并受code review)
的约束