我正在创建一个用于课程作业的Visual Basic座位预订系统,它必须允许用户从A行到E行预订座位。对于B行,我设置它以便没有剩余座位,但是消息只是告诉用户没有足够的座位,并且最大可用座位数为0.我需要代码告诉用户没有剩余座位。我的代码如下: 公共类Form1
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rowNumber As Integer
Dim SeatsData As String(,) = {{"booked", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10"},
{"booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked"},
{"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"},
{"D1", "booked", "booked", "D4", "D5", "D6", "D7", "D8", "D9", "D10"},
{"E1", "E2", "booked", "booked", "booked", "E6", "E7", "E8", "E9", "E10"}}
If ListBox2.Text = "A" Then
rowNumber = 0
ElseIf ListBox2.Text = "B" Then
rowNumber = 1
ElseIf ListBox2.Text = "C" Then
rowNumber = 2
ElseIf ListBox2.Text = "D" Then
rowNumber = 3
ElseIf ListBox2.Text = "E" Then
rowNumber = 4
End If
FindSeats(rowNumber, ListBox1.Text, SeatsData)
End Sub
Function FindSeats(ByVal RowNumber As Integer, ByVal NumSeats As Integer, SeatsData As Array) As String
Dim i As Integer = 0
Dim arrayPos As Integer = 0
Dim maxSeats As Integer = 0
Dim FirstSeat As String = 0
Dim LastSeat As String = 0
Dim Seatsfound As Boolean = False
Dim returnMsg As String = ""
Do While Seatsfound = False
Dim seatChar As String = SeatsData(RowNumber, arrayPos)
arrayPos = arrayPos + 1
If seatChar = "booked" Then
i = 0
Else
If i = 0 Then
FirstSeat = seatChar
End If
i = i + 1
If i > maxSeats Then
maxSeats = i
End If
End If
If i = NumSeats Then
LastSeat = seatChar
If FirstSeat = LastSeat Then
returnMsg = "Found seat: " + FirstSeat
Else
returnMsg = "Found seats: " + FirstSeat + " - " + LastSeat
End If
MsgBox(returnMsg)
Label3.Text = returnMsg
Seatsfound = True
Exit Do
End If
If arrayPos = 10 Then
returnMsg = "Not enough available seats, maximum available seats: " + CStr(maxSeats)
MsgBox(returnMsg)
Label3.Text = returnMsg
Exit Do
End If
Loop
End Function
End Class
如何设置我的代码,以便它告诉用户“此行中没有剩余席位”或其他内容?
答案 0 :(得分:0)
不使用自定义类来设计问题域,一种简单的方法是使用词典
例如
Sub Main
Dim seats = new Dictionary(Of string, List(Of String))()
seats.Add("A", new List(Of String) FROM {"A1", "A2", "A3"})
seats.Add("B", new List(Of String) FROM {"booked", "booked", "booked"})
seats.Add("C", new List(Of String) FROM {"C1", "C2", "booked"})
seats.Add("D", new List(Of String) FROM {"booked", "booked", "D3"})
Dim result = FindSeat(seats, "D")
Console.WriteLine(result) ' Output = D3
result = FindSeat(seats, "B")
Console.WriteLine(result) ' Output = "No free seat on that line"
End Sub
Function FindSeat(seats as Dictionary(Of String, List(Of String)), line as String) As String
if seats.ContainsKey(line) Then
dim seatLine = seats(line)
Dim placeFree = seatLine.FirstOrDefault(Function (x) x <> "booked")
if placeFree Is Nothing then
return "No free seat on that line"
else
return placeFree
End If
Else
return "Not a valid line letter"
End If
End Function
在Main
中,我已静态声明一个包含Key
行号的词典,而List(Of String)
用作声明席位名称及其当前状态的值。
现在要找到一个免费座位,我得到与线路信件相对应的List(Of String)
,并应用一个简单的FirstOrDefault来获得第一个未预订的座位