login  Naam:   Wachtwoord: 
Registreer je!
 Forum

Visual Basic - De uitkomst klopt, behalve 1 regel. Wie kan helpen ? (Opgelost)

Offline DavidWebb - 09/05/2012 19:16 (laatste wijziging 09/05/2012 20:44)
Avatar van DavidWebbLid Hallo,

De code hieronder levert op:

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

  1. Sub Main()
  2.  
  3. Dim zipcodes() As String = {"1000", "Brussel", "2000", "Antwerpen", "3000", "Leuven", "8000", "Brugge", "9000", "Gent"}
  4. Dim zipcodesCount As Integer = 5
  5. Dim searchValues() As String = {"0500", "1000", "2000", "3000", "4000", "8000", "9000", "9500"}
  6.  
  7. For searchValueIndex = 0 To 7
  8. Dim searchValue As String = searchValues(searchValueIndex)
  9. Dim index As Integer
  10. Dim found As Boolean = False
  11.  
  12. index = Array.BinarySearch(zipcodes, searchValue)
  13.  
  14. If index > -1 Then found = True
  15.  
  16. index = 0
  17. Do
  18. If searchValue = zipcodes(index * 2) Then
  19. found = True : index = index * 2
  20. Else
  21. index += 1
  22. End If
  23. Loop Until index = zipcodesCount Or found
  24.  
  25. If found Then
  26. Console.WriteLine(searchValue & " is zipcode of " & zipcodes(index + 1)) Else
  27. Console.WriteLine(searchValue & " not found")
  28. End If
  29. Next
  30. Console.ReadLine()
  31. End Sub

2 antwoorden

Gesponsorde links
Offline Ontani - 10/05/2012 09:39
Avatar van Ontani Gouden medailleGouden medailleGouden medailleGouden medaille

-1
volgens mij ben je beter af met een dictionary:

  1. Dim dic as new Dictionary(Of String, String) From {
  2. {"1000", "Brussel"},
  3. {"2000", "Antwerpen"},
  4. {"3000", "Leuven"},
  5. {"8000", "Brugge"},
  6. {"9000", "Gent"}
  7. }
  8.  
  9. Dim searchValues() As String = {"0500", "1000", "2000", "3000", "4000", "8000", "9000", "9500"}
  10.  
  11. For Each searchValue as String in searchValues()
  12. If dic.containsKey(searchValue) Then
  13. Console.WriteLine(searchValue & " is the zipcode of " & dic(searchValue))
  14. Else
  15. Console.WriteLine(searchValue & " not found"
  16. End If
  17. Next
Bedankt door: DavidWebb
Offline DavidWebb - 11/05/2012 18:52
Avatar van DavidWebb Lid Hallo,

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.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.183s