Autor Beitrag
gamer94
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 41

Win7 HP, WinXP
Delphi 7 Pers.
BeitragVerfasst: Sa 21.07.07 19:27 
Hi, ich hab habe hier ein kleines problem mit einer For Schleife
Mein Code:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
const
  driver = 'data\driver.txt';

var
  Form1: TForm1;
  fahrer: TStringList;
  nr, fahrerzahl: Integer;

...

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i: Integer;
begin
for i:=0 to 5 do
randomize;
fahrer:= TStringList.Create;
fahrer.LoadFromFile(driver);
nr := random(fahrer.Count);
ListBox5.Items.Add(fahrer.Strings[nr]);
end;


Aber aus irgendeinem Grund, wird das ganze nicht 5 mal wiederhohlt. Da ich schon ne ganze Weile am PC sitze, könnte es sein, dass ich einfach nur Müde bin und ich deswegen den Fehler nicht finde.
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 21.07.07 19:33 
Der Inhalt der Schleife wird genau 5 mal wiederholt und 6 mal ausgeführt.
gamer94 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 41

Win7 HP, WinXP
Delphi 7 Pers.
BeitragVerfasst: Sa 21.07.07 19:34 
Und was soll das Bedeuten?
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Sa 21.07.07 19:35 
ausblenden Delphi-Quelltext
1:
do begin					

_________________
Blackheart666
Der Irrsinn ist bei Einzelnen etwas Seltenes, - aber bei Gruppen, Parteien, Völkern, Zeiten die Regel. (Friedrich Nietzsche)
Calculon
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 676

Win XP Professional
Delphi 7 PE, Delphi 3 PRO
BeitragVerfasst: Sa 21.07.07 19:38 
Hätte nie gedacht, dass ich das je schreibe, aber au das tut weh ;-)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.Timer1Timer(Sender: TObject); 
var 
  i: Integer; 
begin 
for i:=0 to 5 do 
randomize; 
fahrer:= TStringList.Create; 
fahrer.LoadFromFile(driver); 
nr := random(fahrer.Count); 
ListBox5.Items.Add(fahrer.Strings[nr]); 
end;


Ich weiß nicht genau was du mit dem Code bezweckst aber folgende Hinweise solltest du befolgen:
(1) randomize nur einmal aufrufen (z.B. in onCreate)
(2) von 0 bis 5 sind 6 Durchläufe!
(3) begin fehlt nach do und auch das end;
(3) TStringList.Create würde ich auch nicht jedesmal im Timer ausführen und des fahrer.free solltest du auch nicht vergessen

Überhaupt kapier' ich gar nicht wieso du 'data\driver.txt' 5mal in einem TimerIntervall hintereinander hereinladen musst. Reicht doch einmal, oder nicht?
Chryzler
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1097
Erhaltene Danke: 2



BeitragVerfasst: Sa 21.07.07 19:41 
user profile icongamer94 hat folgendes geschrieben:
Und was soll das Bedeuten?

Das bedeutet das, was ich geschrieben habe. Also ich würde das so machen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.Timer1Timer(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to 5 do
  begin       
    nr := Random(fahrer.Count);
    ListBox5.Items.Add(fahrer.Strings[nr]);
  end;
end;

procedure TForm1.Form1Create(Sender: TObject);
begin
  Randomize;
  fahrer := TStringList.Create;
  fahrer.LoadFromFile(driver);
end;

Also genau so, wie user profile iconCalculon. :)

Und wenn du willst, dass die Schleife 5 mal durchläuft, dann musst du aus der Null eine Eins machen, oder aus der Fünf eine Vier.
gamer94 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 41

Win7 HP, WinXP
Delphi 7 Pers.
BeitragVerfasst: So 22.07.07 10:09 
:autsch: Danke für eure Hilfe, es funktioniert. :dance2: