如何防止FormatException和IndexOutOfRangeException
本文关键字:IndexOutOfRangeException FormatException 何防止 | 更新日期: 2023-09-27 18:27:57
当用户在将字符串转换为int时输入错误时,此代码会引发Format Exception。当售出32张门票时,它也没有达到所需的输出"所有头等舱座位都已预订",它只是引发了一个索引超出范围的异常。如有任何帮助,我们将不胜感激。谢谢
class FirstClassCompartment
{
public void FirstClassTickets()
{
bool[] seats = new bool[32];
List<int> seatsBooked = new List<int>();
int completeBooking = 0;
bool quit = false;
string ticketAmount = "";
int firstClassSeat = 0;
int convertedTicketAmount;
{
Groups firstClass = new Groups();
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();
Console.Clear();
Console.WriteLine("'nFirst Class booking menu");
convertedTicketAmount = Convert.ToInt32(ticketAmount);
ticketAmount = firstClass.NumberInGroupFirstClassWest;
}
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:
if (seatsBooked.Count == 0)
{
firstClassSeat++;
seats[firstClassSeat] = true;
seatsBooked.Add(firstClassSeat);
}
else
{
do
{
firstClassSeat++;
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("'nPlease enter a valid input");
quit = true;
break;
}
} while (!quit);
}
}
您需要使用Int32.TryParse并检查返回的bool
。
看看switch语句中的逻辑,您只处理1的预订。如果用户预订了2个座位怎么办
编辑:对不起,刚刚意识到切换语句是处理菜单,而不是预订数量
您是否需要int
的列表以及bool
的数组来跟踪已预订的座位数量?你就不能用一个普通的旧int
,例如numberOfSeatsBooked
吗?然后您可以执行numberOfSeatsBooked += convertedTicketAmount;
我会用非常不同的方式来做这件事。您应该有一个座位类,该类包含预订的布尔值,并迭代该布尔值,而不是一个随机的书籍数组,该数组可能链接到或不链接到预订座位和预订座位数量的随机列表。这里有一个小例子:
public class Seat
{
public int SeatNo { get; set; }
public bool FirstClass { get; set ; }
public bool Booked { get; set; } // could actually be an instance of a booking info class including name and ticket id, etc.
}
public class Flight
{
public List<Seat> Seats = new List<Seat>();
private int _lastSeat = 0;
public void AddSeats(int num, bool firstClass)
{
for (int i = 0; i < num; i++)
{
_lastSeat++;
Seats.Add(new Seat { SeatNo = num, FirstClass = firstClass });
}
}
public int BookNextAvailableSeat(bool firstClass)
{
foreach (Seat seat in Seats.AsQueryable().Where(x => x.FirstClass == firstClass)
{
if (!seat.Booked)
{
seat.Booked = true;
return seat.SeatNo;
}
}
return null;
}
}
然后你的:
case 1:
int booking = flight.BookNextAvailableSeat(true);
if (booking != null)
Console.WriteLine("Your seat: {0}", booking);
else
Console.WriteLine("All seats for first class have been booked");
Console.WriteLine("Please press enter to continue...");
break;
当然,您需要实例化航班并使用添加32个座位
Flight flight = new Flight();
flight.AddSeats(32, true);