Rekursion endet ohne ersichtlichen Grund
Übersicht

Karin1989Betreff: Rekursion endet ohne ersichtlichen Grund |
![]() Antworten mit Zitat ![]() |
|
---|---|---|
Hallo Ihr,
vielleicht könnt ihr mir helfen. Ich habe folgendes Problem: Die Rekursion (in Funktion "ausgabe") bricht vorzeitig ab. Ich weis aber nicht warum. Ich erhalte folgende Ausgabe: root one two ... anstatt root one two three four Meine Frage, warum ist das so und wie kann ich das beheben? - Ich danke euch schonmal für eure Zeit und eure Hilfe! Code: [AUSKLAPPEN] Type TTest
Field name:String Field child_count:Byte Field parent:TTest Field child:TTest[10] Function Create:TTest(root:TTest = Null, name:String) Local newTest:TTest = Null If root = Null root = New TTest root.parent = Null ElseIf (root.child_count < 10) newTest = New TTest root.child[root.child_count] = newTest root.child_count = root.child_count + 1 newTest.parent = root root = newTest Else Return Null EndIf root.name = name Return root End Function Function ausgabe(root:TTest) Local i:Int If (root = Null) Then Return EndIf print root.name For i = 0 To i < root.child_count TTest.ausgabe(root.child[i]) Next End Function End Type Local root:TTest = TTest.Create(,"root") Local one:TTest = TTest.Create(root,"one") Local two:TTest = TTest.Create(root,"two") Local three:TTest = TTest.Create(root,"three") Local four:TTest = TTest.Create(root,"four") TTest.ausgabe(root) |
||
![]() |
HolzchopfMeisterpacker |
![]() Antworten mit Zitat ![]() |
---|---|---|
Versuch mal die For-Schleife so aufzubauen
BlitzMax: [AUSKLAPPEN] For i = 0 To root.child_count -1 Ich hab' den Code jetzt nicht getestet, aber ich denke, dass du dein Ergebnis erhältst, weil du mit deiner Endbedingung (i < root.child_count) quasi sagst, dass er von 0 bis 1 (inkl) gehen soll. Denn der logische Ausdruck gibt eine 1 zurück (i ist ja kleiner als child_count) und deshalb gibts nur root, child[0] und child[1] aus. mfG |
||
Erledige alles Schritt um Schritt - erledige alles. - Holzchopf
CC BY ♫ BinaryBorn - Yogurt ♫ (31.10.2018) Im Kopf da knackt's und knistert's sturm - 's ist kein Gedanke, nur ein Wurm |
![]() |
klin |
![]() Antworten mit Zitat ![]() |
---|---|---|
Einfach anstatt for das einfügen:
Code: [AUSKLAPPEN] While i < root.child_count
TTest.ausgabe(root.child[i]) i:+1 Wend Weil damit gehts wenigstens ![]() MFG Klin Edit: Mist zu langsam ![]() |
||
![]() |
FireballFlame |
![]() Antworten mit Zitat ![]() |
---|---|---|
Holzchopf hat Folgendes geschrieben: Versuch mal die For-Schleife so aufzubauen
BlitzMax: [AUSKLAPPEN] For i = 0 To root.child_count -1 Schönere Variante: BlitzMax: [AUSKLAPPEN] For i = 0 Until root.child_count |
||
PC: Intel Core i7 @ 4x2.93GHz | 6 GB RAM | Nvidia GeForce GT 440 | Desktop 2x1280x1024px | Windows 7 Professional 64bit
Laptop: Intel Core i7 @ 4x2.00GHz | 8 GB RAM | Nvidia GeForce GT 540M | Desktop 1366x768px | Windows 7 Home Premium 64bit |
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group