JavaScript in VB.NET übersetzen

  • VB.NET

Es gibt 12 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    JavaScript in VB.NET übersetzen

    Hallo,
    ich habe ein (vermutlich) JavaScript, das würde ich gerne in VB.NET übersetzen...
    Allerdings benötige ich etwas Hilfe...
    Mit online-Convertern war ich nicht erfolgreich.
    Das JavaScript ist zum Lösen von Sudokus.

    Ist es erwünscht dass ich das JavaScript und meinen Versuch in VB.NET poste?

    Vielen Dank!

    hier das JavaScript:

    JavaScript-Quellcode

    1. function sudokuSolver(matrix) {
    2. if (solveSudoku(matrix) === true) {
    3. return matrix;
    4. }
    5. return 'NO SOLUTION';
    6. }
    7. const UNASSIGNED = 0;
    8. function solveSudoku(matrix) {
    9. let row = 0;
    10. let col = 0;
    11. let checkBlankSpaces = false;
    12. /* verify if sudoku is already solved and if not solved,
    13. get next "blank" space position */
    14. for (row = 0; row < matrix.length; row++) {
    15. for (col = 0; col < matrix[row].length; col++) {
    16. if (matrix[row][col] === UNASSIGNED) {
    17. checkBlankSpaces = true;
    18. break;
    19. }
    20. }
    21. if (checkBlankSpaces === true) {
    22. break;
    23. }
    24. }
    25. // no more "blank" spaces means the puzzle is solved
    26. if (checkBlankSpaces === false) {
    27. return true;
    28. }
    29. // try to fill "blank" space with correct num
    30. for (let num = 1; num <= 9; num++) {
    31. /* isSafe checks that num isn't already present
    32. in the row, column, or 3x3 box (see below) */
    33. if (isSafe(matrix, row, col, num)) {
    34. matrix[row][col] = num;
    35. if (solveSudoku(matrix)) {
    36. return true;
    37. }
    38. /* if num is placed in incorrect position,
    39. mark as "blank" again then backtrack with
    40. a different num */
    41. matrix[row][col] = UNASSIGNED;
    42. }
    43. }
    44. return false;
    45. }
    46. function isSafe(matrix, row, col, num) {
    47. return (
    48. !usedInRow(matrix, row, num) &&
    49. !usedInCol(matrix, col, num) &&
    50. !usedInBox(matrix, row - (row % 3), col - (col % 3), num)
    51. );
    52. }
    53. function usedInRow(matrix, row, num) {
    54. for (let col = 0; col < matrix.length; col++) {
    55. if (matrix[row][col] === num) {
    56. return true;
    57. }
    58. }
    59. return false;
    60. }
    61. function usedInCol(matrix, col, num) {
    62. for (let row = 0; row < matrix.length; row++) {
    63. if (matrix[row][col] === num) {
    64. return true;
    65. }
    66. }
    67. return false;
    68. }
    69. function usedInBox(matrix, boxStartRow, boxStartCol, num) {
    70. for (let row = 0; row < 3; row++) {
    71. for (let col = 0; col < 3; col++) {
    72. if (matrix[row + boxStartRow][col + boxStartCol] === num) {
    73. return true;
    74. }
    75. }
    76. }
    77. return false;
    78. }
    79. const sudokuGrid = [
    80. [5, 3, 0, 0, 7, 0, 0, 0, 0],
    81. [6, 0, 0, 1, 9, 5, 0, 0, 0],
    82. [0, 9, 8, 0, 0, 0, 0, 6, 0],
    83. [8, 0, 0, 0, 6, 0, 0, 0, 3],
    84. [4, 0, 0, 8, 0, 3, 0, 0, 1],
    85. [7, 0, 0, 0, 2, 0, 0, 0, 6],
    86. [0, 6, 0, 0, 0, 0, 2, 8, 0],
    87. [0, 0, 0, 4, 1, 9, 0, 0, 5],
    88. [0, 0, 0, 0, 8, 0, 0, 7, 9]
    89. ];
    90. console.log(sudokuSolver(sudokuGrid));


    ----------------------------------------------------------------------------------------
    hier mein VB.NET-Versuch:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Dim row As Integer, col As Integer, num As Integer
    3. Dim checkBlankSpaces As Boolean
    4. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    5. End Sub
    6. Function sudokuSolver(matrix As Integer) As Integer
    7. Dim a As String
    8. If (solveSudoku(matrix) = True) Then
    9. Return matrix
    10. End If
    11. a = "NO SOLUTION"
    12. Return a
    13. End Function
    14. Function solveSudoku(matrix As Integer)
    15. row = 0
    16. col = 0
    17. checkBlankSpaces = False
    18. For row = 1 To 9
    19. For col = 1 To 9
    20. If (matrix[row][col] = 0) Then
    21. checkBlankSpaces = True
    22. break
    23. End If
    24. Next
    25. If (checkBlankSpaces = True) Then
    26. break
    27. End If
    28. Next
    29. If (checkBlankSpaces = False) Then
    30. Return True
    31. End If
    32. For num = 1 To 9
    33. If (isSafe(matrix, row, col, num)) Then
    34. matrix[row][col] = num
    35. If (solveSudoku(matrix)) Then
    36. Return True
    37. End If
    38. matrix[row][col] = 0
    39. End If
    40. Next
    41. Return False
    42. End Function
    43. Function isSafe(matrix, row, col, num)
    44. Return (!usedInRow(matrix, row, num) & !usedInCol(matrix, col, num) &
    45. !usedInBox(matrix, row - (row % 3), col - (col % 3), num)
    46. End Function
    47. Function usedInRow(matrix, row, num)
    48. For (col = 0; col < matrix.length; col++) {
    49. If (matrix[row][col] === num) {
    50. Return True
    51. End If
    52. Next
    53. Return False
    54. End Function
    55. Function usedInCol(matrix, col, num)
    56. For (row = 0; row < matrix.length; row++
    57. If (matrix[row][col] === num) {
    58. Return True;
    59. End If
    60. Return False
    61. End Function
    62. Function usedInBox(matrix, boxStartRow, boxStartCol, num) {
    63. For (row = 0; row < 3; row++) {
    64. For (col = 0; col < 3; col++) {
    65. If (matrix[row + boxStartRow][col + boxStartCol] === num) {
    66. Return True
    67. End If
    68. Next
    69. Next
    70. Return False
    71. End Function
    72. Const sudokuGrid = [[5, 3, 0, 0, 7, 0, 0, 0, 0],[6, 0, 0, 1, 9, 5, 0, 0, 0],[0, 9, 8, 0, 0, 0, 0, 6, 0],[8, 0, 0, 0, 6, 0, 0, 0, 3],[4, 0, 0, 8, 0, 3, 0, 0, 1],[7, 0, 0, 0, 2, 0, 0, 0, 6],[0, 6, 0, 0, 0, 0, 2, 8, 0],[0, 0, 0, 4, 1, 9, 0, 0, 5],[0, 0, 0, 0, 8, 0, 0, 7, 9]]
    73. console.log(sudokuSolver(sudokuGrid))
    74. End Class



    kwon

    *Topic verschoben, korrekte Code-Tags eingefügt*

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Marcus Gräfe“ ()

    kwon schrieb:

    Ist es erwünscht dass ich das JavaScript und meinen Versuch in VB.NET poste?
    Ja, bitte. Es wird wohl keiner Dir ohne Weiteres einen Sudoku-Solver schreiben.
    Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „VaporiZed“, mal wieder aus Grammatikgründen.

    Aufgrund spontaner Selbsteintrübung sind all meine Glaskugeln beim Hersteller. Lasst mich daher bitte nicht den Spekulatiusbackmodus wechseln.
    @kwon Irgendwann einst gab es mal einen Java => C#-Konverter.
    Damit habe ich mal ein Java-Programm (kein Script) nach C# übersetzt und dann zu Fuß compile-fähig gemacht.
    Das gibt es wohl nicht mehr.
    Wenn, dann solltest Du Java nach C# übertragen (die Syntax ist da sehr viel ähnlicher) und danach den C#-Code nach VB.NET übersetzen.
    Jou, häng mal das Java-Script an, um es mal anzusehen.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Hallo,
    danke für die Antworten!!!
    Ich habe einen Java to VB Converter online gefunden... Der Code scheint wohl doch Java zu sein und nicht JavaScript, bin mir nicht sicher.

    Bei den functions weiß ich leider nicht in welchem Datentyp ich sie deklarieren soll (wahrscheinlich Integer??)

    Das gibt der Converter aus (habe auch manuell etwas angepasst):

    VB.NET-Quellcode

    1. ​Public Class Form1
    2. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    3. End Sub
    4. Function sudokuSolver(matrix)
    5. Dim a As String
    6. If (solveSudoku(matrix) = True) Then
    7. Return matrix
    8. End If
    9. a = "NO SOLUTION."
    10. Return a
    11. End Function
    12. Const UNASSIGNED = 0
    13. Function solveSudoku(matrix)
    14. Dim row As Integer = 0
    15. Dim col As Integer = 0
    16. Dim checkBlankSpaces As Boolean = False
    17. ' verify if sudoku is already solved and if not solved,
    18. ' get next "blank" space position
    19. For row = 0 To matrix.length - 1
    20. col = 0
    21. Do While col < matrix(row).length
    22. If matrix(row)(col) = UNASSIGNED Then
    23. checkBlankSpaces = True
    24. Exit Do
    25. End If
    26. col += 1
    27. Loop
    28. If checkBlankSpaces = True Then
    29. Exit For
    30. End If
    31. Next row
    32. ' no more "blank" spaces means the puzzle is solved
    33. If checkBlankSpaces = False Then
    34. Return True
    35. End If
    36. ' try to fill "blank" space with correct num
    37. For num As Integer = 1 To 9
    38. ' isSafe checks that num isn't already present
    39. ' in the row, column, or 3x3 box (see below)
    40. If isSafe(matrix, row, col, num) Then
    41. matrix(row)(col) = num
    42. If solveSudoku(matrix) Then
    43. Return True
    44. End If
    45. ' if num is placed in incorrect position,
    46. ' mark as "blank" again then backtrack with
    47. ' a different num
    48. matrix(row)(col) = UNASSIGNED
    49. End If
    50. Next num
    51. Return False
    52. End Function
    53. Function isSafe(matrix, row, col, num)
    54. If True Then
    55. Return (Not usedInRow(matrix, row, num) AndAlso Not usedInCol(matrix, col, num) AndAlso Not usedInBox(matrix, row - (row Mod 3), col - (col Mod 3), num))
    56. End If
    57. End Function
    58. Function usedInRow(matrix, row, num)
    59. For col As Integer = 0 To matrix.length - 1
    60. If matrix(row)(col) = num Then
    61. Return True
    62. End If
    63. Next col
    64. Return False
    65. End Function
    66. Function usedInCol(matrix, col, num)
    67. For row As Integer = 0 To matrix.length - 1
    68. If matrix(row)(col) = num Then
    69. Return True
    70. End If
    71. Next row
    72. Return False
    73. End Function
    74. Function usedInBox(matrix, boxStartRow, boxStartCol, num)
    75. For row As Integer = 0 To 2
    76. For col As Integer = 0 To 2
    77. If matrix(row + boxStartRow)(col + boxStartCol) = num Then
    78. Return True
    79. End If
    80. Next col
    81. Next row
    82. Return False
    83. End Function
    84. Dim matrix As Integer = ((5, 3, 0, 0, 7, 0, 0, 0, 0), (6, 0, 0, 1, 9, 5, 0, 0, 0), (0, 9, 8, 0, 0, 0, 0, 6, 0), (8, 0, 0, 0, 6, 0, 0, 0, 3), (4, 0, 0, 8, 0, 3, 0, 0, 1), (7, 0, 0, 0, 2, 0, 0, 0, 6), (0, 6, 0, 0, 0, 0, 2, 8, 0), (0, 0, 0, 4, 1, 9, 0, 0, 5), (0, 0, 0, 0, 8, 0, 0, 7, 9))
    85. console.log(sudokuSolver(matrix))
    86. End Class


    Vielleicht ist jetzt jemand doch so freundlich und könnte mal drüberschauen??

    Ich möchte bescheidsagen dass ich einen Converter Java->VB gefunden habe...

    Viele Grüße,
    kwon

    kwon schrieb:

    Der Code scheint wohl doch Java zu sein und nicht JavaScript

    Sieht für mich aber stark nach JavaScript aus, nicht Java. Und die beiden Sprachen haben außer dem Namen nichts gemeinsam (abgesehen vom Syntax vielleicht).

    kwon schrieb:

    Vielleicht ist jetzt jemand doch so freundlich und könnte mal drüberschauen??

    Es wäre wohl sinnvoller, wenn du sagst, an welchen Stellen es hakt.
    Besucht auch mein anderes Forum:
    Das Amateurfilm-Forum
    @RodFromGermany
    das gibt der converter (java to vb) aus wenn ich den code aus post 1 konvertiere

    VB.NET-Quellcode

    1. ​Friend UNASSIGNED As const = 0
    2. 'JAVA TO VB CONVERTER TODO TASK: The following line could not be converted:
    3. [function] solveSudoku(matrix)
    4. If True Then
    5. Dim row As [let] = 0
    6. Dim col As [let] = 0
    7. Dim checkBlankSpaces As [let] = False
    8. ' verify if sudoku is already solved and if not solved,
    9. ' get next "blank" space position
    10. For row = 0 To matrix.length - 1
    11. col = 0
    12. Do While col < matrix(row).length
    13. If matrix(row)(col) == UNASSIGNED Then
    14. checkBlankSpaces = True
    15. Exit Do
    16. End If
    17. col += 1
    18. Loop
    19. If checkBlankSpaces == True Then
    20. Exit For
    21. End If
    22. Next row
    23. ' no more "blank" spaces means the puzzle is solved
    24. If checkBlankSpaces == False Then
    25. Return True
    26. End If
    27. ' try to fill "blank" space with correct num
    28. For num As [let] = 1 To 9
    29. ' isSafe checks that num isn't already present
    30. ' in the row, column, or 3x3 box (see below)
    31. If isSafe(matrix, row, col, num) Then
    32. matrix(row)(col) = num
    33. If solveSudoku(matrix) Then
    34. Return True
    35. End If
    36. ' if num is placed in incorrect position,
    37. ' mark as "blank" again then backtrack with
    38. ' a different num
    39. matrix(row)(col) = UNASSIGNED
    40. End If
    41. Next num
    42. Return False
    43. End If
    44. [function] isSafe(matrix, row, col, num)
    45. If True Then
    46. Return (Not usedInRow(matrix, row, num) AndAlso Not usedInCol(matrix, col, num) AndAlso Not usedInBox(matrix, row - (row Mod 3), col - (col Mod 3), num))
    47. End If
    48. [function] usedInRow(matrix, row, num)
    49. If True Then
    50. For col As [let] = 0 To matrix.length - 1
    51. If matrix(row)(col) == num Then
    52. Return True
    53. End If
    54. Next col
    55. Return False
    56. End If
    57. [function] usedInCol(matrix, col, num)
    58. If True Then
    59. For row As [let] = 0 To matrix.length - 1
    60. If matrix(row)(col) == num Then
    61. Return True
    62. End If
    63. Next row
    64. Return False
    65. End If
    66. [function] usedInBox(matrix, boxStartRow, boxStartCol, num)
    67. If True Then
    68. For row As [let] = 0 To 2
    69. For col As [let] = 0 To 2
    70. If matrix(row + boxStartRow)(col + boxStartCol) == num Then
    71. Return True
    72. End If
    73. Next col
    74. Next row
    75. Return False
    76. End If
    77. Dim matrix As const = {(5, 3, 0, 0, 7, 0, 0, 0, 0), (6, 0, 0, 1, 9, 5, 0, 0, 0), (0, 9, 8, 0, 0, 0, 0, 6, 0), (8, 0, 0, 0, 6, 0, 0, 0, 3), (4, 0, 0, 8, 0, 3, 0, 0, 1), (7, 0, 0, 0, 2, 0, 0, 0, 6), (0, 6, 0, 0, 0, 0, 2, 8, 0), (0, 0, 0, 4, 1, 9, 0, 0, 5), (0, 0, 0, 0, 8, 0, 0, 7, 9)}
    78. console.log(sudokuSolver(matrix))


    diese funktion konnte nicht übersetzt werden

    Quellcode

    1. ​function sudokuSolver(matrix) {
    2. if (solveSudoku(matrix) === true) {
    3. return matrix;
    4. }
    5. return 'NO SOLUTION';
    6. }


    @Marcus Gräfe
    Ich weiß leider nicht ob der Java zu VB Konverter sinnvoll ist, wenn es JavaScript ist

    Entschuldigung vielmals für die Verwirrung...
    Es tut mir leid, dass ich einige Umstände mache...

    kwon schrieb:

    diese funktion konnte nicht übersetzt werden
    Klar.
    Etwa so was:

    VB.NET-Quellcode

    1. Function sudokuSolver(matrix As Integer()()) As Integer()()
    2. If solveSudoku(matrix) Then
    3. Return matrix
    4. End If
    5. Return Nothing
    6. End Function
    7. Private Function solveSudoku(matrix As Integer()()) As Boolean
    8. Throw New NotImplementedException
    9. End Function
    =============
    Schau mal hier rein:
    Sudoku Solver
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „RodFromGermany“ ()

    Hallo RodFromGermany,

    vielen Dank für deine Hilfe!!

    VB.NET-Quellcode

    1. Public Class Form1
    2. Dim row As Integer, col As Integer
    3. Dim matrix As Integer()() = ((5, 3, 0, 0, 7, 0, 0, 0, 0), (6, 0, 0, 1, 9, 5, 0, 0, 0), (0, 9, 8, 0, 0, 0, 0, 6, 0), (8, 0, 0, 0, 6, 0, 0, 0, 3), (4, 0, 0, 8, 0, 3, 0, 0, 1), (7, 0, 0, 0, 2, 0, 0, 0, 6), (0, 6, 0, 0, 0, 0, 2, 8, 0), (0, 0, 0, 4, 1, 9, 0, 0, 5), (0, 0, 0, 0, 8, 0, 0, 7, 9))
    4. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    5. End Sub
    6. Function sudokuSolver(matrix As Integer()()) As Integer()()
    7. If solveSudoku(matrix) Then
    8. Return matrix
    9. End If
    10. Return Nothing
    11. End Function
    12. Private Function solveSudoku(matrix As Integer()()) As Boolean
    13. Throw New NotImplementedException
    14. End Function


    in Zeile 3 ist mir leider nicht klar wie ich matrix deklarieren soll

    in Zeile 9 und 14 klemmt es: in zeile 9 heißt es "nicht spezifisch genug"

    Vielen Dank!!
    Bilder
    • bild.jpg

      59,45 kB, 874×163, 51 mal angesehen
    @kwon Probier mal
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Dim matrix As Integer(,) = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, {4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9}}
    2. Function sudokuSolver(matrix As Integer(,)) As Integer(,)
    3. If solveSudoku(matrix) Then
    4. Return matrix
    5. End If
    6. Return Nothing
    7. End Function
    8. Private Function solveSudoku(matrix As Integer(,)) As Boolean
    9. Throw New NotImplementedException
    10. End Function
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Hallo RodFromGermany,

    vielen, vielen Dank für deine Hilfe!!!

    Ich habe versucht den Code zu überarbeiten.
    Typische SudokuSolver zählen oft von 0 bis 8 oder von 1 bis 9 (und lassen das 0te Element unbenutzt).

    VB.NET-Quellcode

    1. ​Public Class Form1
    2. Dim row As Integer, col As Integer
    3. Dim matrix As Integer(,) = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, {4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9}}
    4. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    5. sudokuSolver(matrix)
    6. End Sub
    7. Function sudokuSolver(matrix As Integer(,)) As Integer(,)
    8. If solveSudoku(matrix) Then
    9. Return matrix
    10. End If
    11. Return Nothing
    12. End Function
    13. 'Private Function solveSudoku(matrix As Integer(,)) As Boolean
    14. 'Throw New NotImplementedException
    15. 'End Function
    16. Const UNASSIGNED = 0
    17. Function solveSudoku(matrix As Integer(,)) As Boolean
    18. Dim row As Integer = 0
    19. Dim col As Integer = 0
    20. Dim checkBlankSpaces As Boolean = False
    21. ' verify if sudoku is already solved and if not solved,
    22. ' get next "blank" space position
    23. For row = 0 To matrix.Length
    24. col = 0
    25. Do While col < matrix.Length
    26. If matrix(row, col) = UNASSIGNED Then
    27. checkBlankSpaces = True
    28. Exit Do
    29. End If
    30. col += 1
    31. Loop
    32. If checkBlankSpaces = True Then
    33. Exit For
    34. End If
    35. Next row
    36. ' no more "blank" spaces means the puzzle is solved
    37. If checkBlankSpaces = False Then
    38. Return True
    39. End If
    40. ' try to fill "blank" space with correct num
    41. For num As Integer = 1 To 9
    42. ' isSafe checks that num isn't already present
    43. ' in the row, column, or 3x3 box (see below)
    44. If isSafe(matrix, row, col, num) Then
    45. matrix(row, col) = num
    46. If solveSudoku(matrix) Then
    47. Return True
    48. End If
    49. ' if num is placed in incorrect position,
    50. ' mark as "blank" again then backtrack with
    51. ' a different num
    52. matrix(row, col) = UNASSIGNED
    53. End If
    54. Next num
    55. Return False
    56. End Function
    57. Function isSafe(matrix(,) As Integer, row As Integer, col As Integer, num As Integer) As Boolean
    58. If True Then
    59. Return (Not usedInRow(matrix, row, num) AndAlso Not usedInCol(matrix, col, num) AndAlso Not usedInBox(matrix, row - (row Mod 3), col - (col Mod 3), num))
    60. End If
    61. End Function
    62. Function usedInRow(matrix(,) As Integer, row As Integer, num As Integer) As Boolean
    63. For col As Integer = 0 To matrix.Length - 1
    64. If matrix(row, col) = num Then
    65. Return True
    66. End If
    67. Next col
    68. Return False
    69. End Function
    70. Function usedInCol(matrix(,) As Integer, col As Integer, num As Integer) As Boolean
    71. For row As Integer = 0 To matrix.Length - 1
    72. If matrix(row, col) = num Then
    73. Return True
    74. End If
    75. Next row
    76. Return False
    77. End Function
    78. Function usedInBox(matrix(,) As Integer, boxStartRow As Integer, boxStartCol As Integer, num As Integer) As Boolean
    79. For row As Integer = 0 To 2
    80. For col As Integer = 0 To 2
    81. If matrix(row + boxStartRow, col + boxStartCol) = num Then
    82. Return True
    83. End If
    84. Next col
    85. Next row
    86. Return False
    87. End Function
    88. End Class


    Kann ich die row und col nicht bei 1 beginnen lassen (Zeile 23,24)?
    Und für matrix.length nicht einfach 9 schreiben?

    Vielen, vielen Dank für die Geduld...

    Gruß, kwon

    kwon schrieb:

    Kann ich die row und col nicht bei 1 beginnen lassen
    Du kannst sie auch bei 42 beginnen lassen.
    Allerdings musst Du Dich dann zeilenweise durchhangeln und überall einen Index-Offset einarbeiten.
    Ich weiß nicht, welchen Index das erste Element eines Java-Array trägt, aber in Hinblick auf zukünftige Programmierarbeiten in .NET empfehle ich Dir, mit 0-basierten Indizes zu arbeiten.
    Ansonsten sehen wir uns hier öfter mal wieder mit Folgefehlern bzw. Effekten.
    Da alte Basic-Dialekte 1-basiert waren, kannst Du auch in VB.NET 1-basiert denken und programmieren, da wird das Element mit dem Index 0 einfach übersprungen.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!