【发布时间】:2017-03-17 22:43:09
【问题描述】:
我从文件中读取了一些两列数据并放入列表中,然后按字母顺序排序。
//文件
鹰嘴豆泥,0.75
辣椒,0.50
塔布利,1.25
Tzatziki,0.50
//声明变量和公共属性
Dim extraList As List(Of extra)
Public Class extra
Implements IComparable(Of extra)
Public Property Name As String
Public Property Price As Decimal
'Public Property extraList As List(Of extra)
Public Function CompareTo(other As extra) As Integer Implements IComparable(Of extra).CompareTo
Return Me.Name.CompareTo(other.Name)
End Function
End Class
//将数据放入列表并排序
Sub Get_Extras_List()
'Reads the extras file and puts the information into a list, splitting the name of the extra and the price into separate columns
Dim allExtras = From line In System.IO.File.ReadLines("C:\Users\ExtrasList.txt")
Let Columns = line.Split(","c)
Where Columns.Length = 2
Let Price = Decimal.Parse(Columns(1).Trim())
Let Name = Columns(0).Trim()
Select New extra With {.Name = Name, .Price = Price}
extraList = allExtras.ToList()
'Sort the list alphabetically
extraList.Sort()
End Sub
现在我需要编写一个方法,允许用户输入额外的内容并使用二进制搜索来搜索它以查看它是否存在。到目前为止,我已经尝试过这个,但它只是不起作用,即使它做了我如何让它返回一个真值或假值? (存在与否?)
Sub Search_Extras_List()
Dim strSearchExtra As String = Console.ReadLine()
Console.WriteLine(vbLf & "BinarySearch for '{0}':", strSearchExtra)
Dim index As Integer =
List(Of extra).BinarySearch(extraList.Name, strSearchExtra)
End Sub
最后,我必须让用户选择其中一项附加服务,然后将其价格添加到总价中。我如何参考价格? extraList.Price?额外的价格?等等。
【问题讨论】:
标签: vb.net list binary-search