![]() |
Wer findet den Fehler?
Da mir gerade ein bisschen langweilig ist programmiere ich ein Programm bei dem Autos zufallsgesteuert durch eine Strassenkarte mit mehreren Kreuzungen und Ampelsystemen. Doch das ganze hängt schon beiden Ampeln irgentein Fheler hat sich in den Code eingeschlichen und ich finde ihn nicht.
Code:
DIe Ampel schält nicht sondern bleibt in der ersten situtation stehn
procedure TForm1.AmpelsteuerungTimer(Sender: TObject);
begin if A = 0 then begin ampelsteuerung.Interval:=6000; A:=A+1; rot1.Brush.Color := clred; gelb1.Brush.Color:= clblack; gruen1.Brush.Color:= clblack; rot2.Brush.Color := clblack; gelb2.Brush.Color:= clblack; gruen2.Brush.Color:= cllime; rot.Brush.Color:= clred; gelb.Brush.Color:= clblack; gruen.Brush.Color:= clblack; rot3.Brush.Color:= clblack; gelb3.Brush.Color:= clblack; gruen3.Brush.Color:= cllime; rot6.Brush.Color:= clred; gelb6.Brush.Color:= clblack; gruen6.Brush.Color:= clblack; rot5.Brush.Color:= clblack; gelb5.Brush.Color:= clblack; gruen5.Brush.Color:= cllime; end; if A = 1 then begin ampelsteuerung.Interval:= 1000; A:=A+1; rot1.Brush.Color := clred; gelb1.Brush.Color:= clyellow; gruen1.Brush.Color:= clblack; rot2.Brush.Color := clblack; gelb2.Brush.Color:= clyellow; gruen2.Brush.Color:= clblack; rot.Brush.Color:= clred; gelb.Brush.Color:= clyellow; gruen.Brush.Color:= clblack; rot3.Brush.Color:= clblack; gelb3.Brush.Color:= clyellow; gruen3.Brush.Color:= clblack; rot6.Brush.Color:= clred; gelb6.Brush.Color:= clyellow; gruen6.Brush.Color:= clblack; rot5.Brush.Color:= clblack; gelb5.Brush.Color:= clyellow; gruen5.Brush.Color:= clblack; end; if A = 2 then begin ampelsteuerung.Interval:=6000; A:=A+1; rot1.Brush.Color := clblack; gelb1.Brush.Color:= clblack; gruen1.Brush.Color:= cllime; rot2.Brush.Color := clred; gelb2.Brush.Color:= clblack; gruen2.Brush.Color:= clblack; rot.Brush.Color:= clblack; gelb.Brush.Color:= clblack; gruen.Brush.Color:= cllime; rot3.Brush.Color:= clred; gelb3.Brush.Color:= clblack; gruen3.Brush.Color:= clblack; rot6.Brush.Color:= clblack; gelb6.Brush.Color:= clblack; gruen6.Brush.Color:= cllime; rot5.Brush.Color:= clred; gelb5.Brush.Color:= clblack; gruen5.Brush.Color:= clblack; end; if A = 3 then begin ampelsteuerung.Interval:=1000; A:=A-3; rot1.Brush.Color := clblack; gelb1.Brush.Color:= clyellow; gruen1.Brush.Color:= clblack; rot2.Brush.Color := clred; gelb2.Brush.Color:= clyellow; gruen2.Brush.Color:= clblack; rot.Brush.Color:= clblack; gelb.Brush.Color:= clyellow; gruen.Brush.Color:= clblack; rot3.Brush.Color:= clred; gelb3.Brush.Color:= clyellow; gruen3.Brush.Color:= clblack; rot6.Brush.Color:= clblack; gelb6.Brush.Color:= clyellow; gruen6.Brush.Color:= clblack; rot5.Brush.Color:= clred; gelb5.Brush.Color:= clyellow; gruen5.Brush.Color:= clblack; end; end; |
AW: Wer findet den Fehler?
Was genau geht nicht?
Btw: Hier würde sch eine case anbieten |
AW: Wer findet den Fehler?
Und der Debugger bietet sich auch an.
|
AW: Wer findet den Fehler?
Zitat:
Zitat:
|
AW: Wer findet den Fehler?
Es entsteht ja keine Fehlermeldung sonder es passiert nur nicht was eigentlich passieren sollte
|
AW: Wer findet den Fehler?
Was soll sie denn schälen? Hast Du einmal einen Haltepunkt gesetzt und Dir den Wert von A angeschaut?
|
AW: Wer findet den Fehler?
Delphi-Tags sind auch was Cooles.
Und lies die mal den Thread durch, wo erklärrt wird, wie man eine Frage stellt. Vorallen die berschrift und die Fehlerbeschreibung lassen sehr zu wünschen übrig. Und nun zum Problem: Geh mal im Kopf das Folgende durch. Fange mit A=0 an (und z.B. auch mal mit A=2) und gehe den Code duch ... du wirst sehn was da falasch läuft.
Delphi-Quellcode:
Die Nutzung des Debuggers und das CASE oder alternativ zum CASE das ELSE helfen schon sehr viel, oder man verschiebt das Setzen der Variable A hinter alle IFs.
// alles Unwichtige aus dem Code entfernt
if A = 0 then begin A:=A+1; end; if A = 1 then begin A:=A+1; end; if A = 2 then begin A:=A+1; end; if A = 3 then begin A:=A-3; end; Vorallem der Debugger hilft bei der Aufgabe, welche ich dir oben gestellt hab. |
AW: Wer findet den Fehler?
Delphi-Quellcode:
Entweder mit exit arbeiten oder rückwärts anfangen...
procedure TForm1.FormCreate(Sender: TObject);
begin ampelStatus := 0; end; procedure TForm1.Timer1Timer(Sender: TObject); var ampelRot: array[0..5] of TShape; ampelGelb: array[0..5] of TShape; ampelGruen: array[0..5] of TShape; i: Integer; begin for i := 0 to 5 do begin ampelRot[i] := TShape(FindComponent('rot'+inttostr(i+1))); ampelGelb[i] := TShape(FindComponent('gelb'+inttostr(i+1))); ampelGruen[i] := TShape(FindComponent('gruen'+inttostr(i+1))); end; case ampelStatus of 0: begin for i := 0 to 5 do begin ampelRot[i].Brush.Color := clRed; ampelGelb[i].Brush.Color := clBlack; ampelGruen[i].Brush.Color := clBlack; end; timer1.Interval := 6000; ampelStatus := 1; end; 1: begin for i := 0 to 5 do begin ampelRot[i].Brush.Color := clRed; ampelGelb[i].Brush.Color := clYellow; ampelGruen[i].Brush.Color := clBlack; end; timer1.Interval := 2000; ampelStatus := 2; end; 2: begin for i := 0 to 5 do begin ampelRot[i].Brush.Color := clBlack; ampelGelb[i].Brush.Color := clBlack; ampelGruen[i].Brush.Color := clGreen; end; timer1.Interval := 6000; ampelStatus := 3; end; 3: begin for i := 0 to 5 do begin ampelRot[i].Brush.Color := clBlack; ampelGelb[i].Brush.Color := clYellow; ampelGruen[i].Brush.Color := clBlack; end; timer1.Interval := 2000; ampelStatus := 0; end; end; end; |
AW: Wer findet den Fehler?
Zitat:
PS: Es gibt auch [delphi]-Tags :zwinker: |
AW: Wer findet den Fehler?
Jupp, das CASE macht nur das, was zutrifft und sonst nix.
Bei den vielen IFs hätte man zwar mit EXIT arbeiten können, aber anstatt sowas Krankes zu machen, wäre es mit ELSE besser und vorallem übersichtlicher. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 03:33 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz