0500 not found
1000 is zipcode of Brussel
2000 is zipcode of Antwerpen
3000 is zipcode of 2000
4000 not found
8000 is zipcode of Brugge
9000 is zipcode of Gent
9500 not found
De vierde regel zou moeten zijn :
3000 is zipcode of Leuven
Waar heb ik een fout gemaakt in mijn code ? Ik heb het al verschillende keren overlezen, maar vind het niet.
Alvast bedankt voor de hulp,
David Webb
Sub Main()
Dim zipcodes() As String = {"1000", "Brussel", "2000", "Antwerpen", "3000", "Leuven", "8000", "Brugge", "9000", "Gent"}
Dim zipcodesCount As Integer = 5
Dim searchValues() As String = {"0500", "1000", "2000", "3000", "4000", "8000", "9000", "9500"}
For searchValueIndex = 0 To 7
Dim searchValue As String = searchValues(searchValueIndex)
Dim index As Integer
Dim found As Boolean = False
index = Array.BinarySearch(zipcodes, searchValue)
If index > -1 Then found = True
index = 0
Do
If searchValue = zipcodes(index * 2) Then
found = True : index = index * 2
Else
index += 1
End If
Loop Until index = zipcodesCount Or found
If found Then
Console.WriteLine(searchValue & " is zipcode of " & zipcodes(index + 1)) Else
Console.WriteLine(searchValue & " not found")
End If
Next
Console.ReadLine()
End Sub
Dim dic as new Dictionary(Of String, String) From {
{"1000", "Brussel"},
{"2000", "Antwerpen"},
{"3000", "Leuven"},
{"8000", "Brugge"},
{"9000", "Gent"}
}
Dim searchValues() As String = {"0500", "1000", "2000", "3000", "4000", "8000", "9000", "9500"}
For Each searchValue as String in searchValues()
If dic.containsKey(searchValue) Then
Console.WriteLine(searchValue & " is the zipcode of " & dic(searchValue))
Else
Console.WriteLine(searchValue & " not found"
End If
Next
Alvast bedankt voor deze feedback. Maar ik ben nog niet aan Dictionaries toe en zou het via de old fashioned way willen oplossen om het te verstaan op mijn niveau.
De fout moet zitten in volgend stukje code, maar ik vind het echt niet. Kan iemand hier nog eens voor kijken aub? Bedankt !
index = Array.BinarySearch(zipcodes, searchValue)
If index > -1 Then found = True
index = 0
Do
If searchValue = zipcodes(index * 2) Then
found = True : index = index * 2
Else
index += 1
End If
Loop Until index = zipcodesCount Or found
Gesponsorde links
Je moet ingelogd zijn om een reactie te kunnen posten.