当程序已满时,如何关闭程序
本文关键字:关闭程序 满时 程序 | 更新日期: 2023-09-27 18:26:28
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# 中的乞丐。
好吧,如果您需要"快速而肮脏"的解决方案,请将fullyBooked
变量声明移动到主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
因此,您的主循环将运行,直到所有座位都被预订。
但同样 - 它非常"快速和肮脏"的修复,考虑重构你的实现,因为它看起来不是很好。(我想这里有点跑题,需要代码审查(