Autor Beitrag
Spurius
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 142

Win XP Home
D3 Prof, D7 Prof
BeitragVerfasst: Fr 25.06.04 16:59 
Hallo,
wie muss ich den folgenden Code ändern, damit anstatt Memo1 List2 da steht?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
var
  Form1: TForm1;
  List, List2: TStringlist;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var a,b,Zahl: integer;
    Disc: boolean;
begin
 randomize;
 List := TStringlist.Create;
 List2 :) TStringlist.Create;

 for a := 1 to 32 do begin
  List.Add(inttostr(a));
 end;

 while memo1.Lines.Count < 8 do begin
  Zahl := random(32);
   for b := 1 to memo1.Lines.Count do begin
    if inttostr(Zahl) = Memo1.Lines[b] then
     Disc := False;
    end;
   if Disc = true then Memo1.Lines.Add(IntToStr(Zahl));
  Disc := true;
 end;
end;
end.

Ich habs schon selbst probiert, aber dann kommen bei mir die Zahlen manchmal doppelt vor.
Sinn des ganzen ist, dass 8 verschiedene Zahlen aus List ausgesucht werden und in List 2 geschrieben werden.
Gruß
Spurius
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Fr 25.06.04 22:02 
Nur oberflächlich getestet, müsste aber gehen in Hinsicht auf:
Spurius hat folgendes geschrieben:
Ich habs schon selbst probiert, aber dann kommen bei mir die Zahlen manchmal doppelt vor.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
procedure TForm1.Button1Click(Sender: TObject);
var a,b,Zahl: integer;
    Disc: boolean;
begin
 randomize;
 List := TStringlist.Create;
 List2 := TStringlist.Create;


 for a := 1 to 32 do begin
  List.Add(inttostr(a));
 end;

 while List2.Count < 8 do begin
  Zahl := random(32);
   for b := 0 to list2.Count - 1 do begin
    if inttostr(Zahl) = List2.Strings[b] then
     Disc := False;
    end;
   if Disc = true then List2.Add(IntToStr(Zahl));
  Disc := true;
 end;
end;

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
StefanH
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1144

Win XP
D5 Standard, D7 Pers, D2005 Pers
BeitragVerfasst: Sa 26.06.04 10:55 
tommie-lie hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
Zahl := random(32);
   for b := 0 to list2.Count - 1 do begin
    if inttostr(Zahl) = List2.Strings[b] then
     Disc := False;
    end;


ausblenden Delphi-Quelltext
1:
if List2.IndexOf(IntToStr(Zahl)<>-1					


ist doch kürzer, oder?

_________________
"Als es noch keine Computer gab, war das Programmieren noch relativ einfach."(Edsger W. Dijkstra)
"Ich bin nicht von Sinnen, sondern ich rede wahre und vernünftige Worte." (Paulus)
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Sa 26.06.04 12:24 
StefanH hat folgendes geschrieben:
ist doch kürzer, oder?
Im Prinzip schon, noch kürzer wäre
Disc := List2.IndexOf(IntToStr(Zahl)) = -1;bzw. noch kürzer
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
while List2.Count < 8 do
begin
  Zahl := random(32);
  if List2.IndexOf(IntToStr(Zahl)) = -1 then
    List2.Add(IntToStr(Zahl));
end;
Aber ich habe einfach den Code von Spurius auf die Verwendung einer Stringlist getrimmt, weder optimiert noch umstrukturiert noch sonstwas mit gemacht, damit er seinen Code noch wiedererkennt ;-)

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
StefanH
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1144

Win XP
D5 Standard, D7 Pers, D2005 Pers
BeitragVerfasst: Sa 26.06.04 19:25 
ich hab mich schon gewundert, wozu er disc überhaupt braucht... :roll:

:mrgreen:

_________________
"Als es noch keine Computer gab, war das Programmieren noch relativ einfach."(Edsger W. Dijkstra)
"Ich bin nicht von Sinnen, sondern ich rede wahre und vernünftige Worte." (Paulus)
Spurius Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 142

Win XP Home
D3 Prof, D7 Prof
BeitragVerfasst: So 27.06.04 08:02 
Hallo,
ich hab mittlerweile eine Lösung. Das Disc war/ist wegen der Überprüfung auf doppelt vorkommende Zahlen.