Autor Beitrag
Blackheart
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 164

ME
D3Prof.-D6Standard
BeitragVerfasst: Mi 19.05.04 23:05 
Hallo
Wie kann ich am einfachsten herausfinden ob bestimmte werte gleich sind also zB.
Ich habe 6 Zufallsvariablen

a,b,c,d,e,f:Integer;

a:Random(6)+1;
...

Jetzt erzeugt er zum Bsp. 1-6-6-2-4-6 oder 5-1-4-5-5-5
nun möchte ich gern im Label anzeigen 3 x Sechs oder 4 x Fünf ohne alle Möglichkeiten mit
if (a=6)and(b=6)then usw. abfragen zu müssen
kann mir dabei vieleicht einer einen Tip geben?
Schon mal Dank im Vorraus.
aM0xACiLLiN
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 64



BeitragVerfasst: Mi 19.05.04 23:37 
so geht's:
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:
//ausgabe: 1-6-6-2-4-6 oder 5-1-4-5-5-5
procedure TForm1.WievielWerte(wert:string);
var nums:array[0..9of integer; c:integer;
begin
for c:=0 to 9 do nums[c]:=0// initialisieren!
for c:=0 to (length(wert)-1do
 if (copy(wert,c+1,1) <> '-'then
  Inc(nums[StrToInt(copy(wert,c+1,1))]);
// falls es nicht geht, das probieren: nums[StrToInt(copy(wert,c+1,1))]:=nums[StrToInt(copy(wert,c+1,1))]+1;
for c:=0 to 9 do
 Memo1.Lines.Add('Es wurden '+IntToStr(nums[c])+' '+IntToStr(c)+'er gefunden');
end;


// aufruf:
var temp:string; a,b,c,d,e,f:integer;
begin
a:=Random(20);
//..
temp:=IntToStr(a)+'-'+IntToStr(b)+'-'+IntToStr(c)+'-'+IntToStr(d)+'-'+IntToStr(e)+'-'+IntToStr(f);
WievielWerte(temp);
end;


du brauchst dazu ein memo auf dem formular, das ergebnis würde so aussehen:
Es wurden 0 0er gefunden
Es wurden 2 1er gefunden
Es wurden 0 2er gefunden
..

cu
IngoD7
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 629


D7
BeitragVerfasst: Do 20.05.04 11:02 
aM0xACiLLiN hat folgendes geschrieben:
so geht's:
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:
//ausgabe: 1-6-6-2-4-6 oder 5-1-4-5-5-5
procedure TForm1.WievielWerte(wert:string);
var nums:array[0..9of integer; c:integer;
begin
for c:=0 to 9 do nums[c]:=0// initialisieren!
for c:=0 to (length(wert)-1do
 if (copy(wert,c+1,1) <> '-'then
  Inc(nums[StrToInt(copy(wert,c+1,1))]);
// falls es nicht geht, das probieren: nums[StrToInt(copy(wert,c+1,1))]:=nums[StrToInt(copy(wert,c+1,1))]+1;
for c:=0 to 9 do
 Memo1.Lines.Add('Es wurden '+IntToStr(nums[c])+' '+IntToStr(c)+'er gefunden');
end;


// aufruf:
var temp:string; a,b,c,d,e,f:integer;
begin
a:=Random(20);
//..
temp:=IntToStr(a)+'-'+IntToStr(b)+'-'+IntToStr(c)+'-'+IntToStr(d)+'-'+IntToStr(e)+'-'+IntToStr(f);
WievielWerte(temp);
end;



Hm ... ob ich das vom Prinzip her so machen würde, weiss ich nicht. Ausserdem wollte er nicht Random(20) haben, sondern er hat Random(6)+1, also einen Würfel. Somit brauchen auch nur die Ziffern 1 bis 6 geprüft werden (und nicht 0 bis 9). Unter diesen Voraussetzungen und unter Beibehaltung des Lösungsansatzes würde ich es so machen (ich lasse mal möglichst viel unverändert stehen):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
//ausgabe: 1-6-6-2-4-6 oder 5-1-4-5-5-5
procedure TForm1.WievielWerte(wert:string);
var nums:array[1..6of integer; c:integer;
begin
for c:=1 to 6 do nums[c]:=0// initialisieren!
for c:=1 to length(wert) do
   Inc(nums[StrToInt(wert[c])]);
for c:=1 to 6 do
 Memo1.Lines.Add('Es wurden '+IntToStr(nums[c])+' '+IntToStr(c)+'er gefunden');
end;


// aufruf:
var temp:string; a,b,c,d,e,f:integer;
begin
a:=Random(6)+1;
//..
temp:=IntToStr(a)+IntToStr(b)+IntToStr(c)+IntToStr(d)+IntToStr(e)+IntToStr(f);
WievielWerte(temp);
end;
Blackheart Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 164

ME
D3Prof.-D6Standard
BeitragVerfasst: Fr 21.05.04 19:18 
Danke für eure schnelle Hilfe
aber so recht steig ich da noch nicht durch.
wollte eigentlich auf die gefundenen Variablen zugreifen um die Punkte im Label anzuzeigen meinetwegen 4 Sechsen =240 Punkte im Label oder 5 Vieren 200 Punkte.
Trotzdem Dank !!!
Blackheart Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 164

ME
D3Prof.-D6Standard
BeitragVerfasst: Fr 21.05.04 19:20 
Danke für eure schnelle Hilfe
aber so recht steig ich da noch nicht durch.
wollte eigentlich auf die gefundenen Variablen zugreifen um die Punkte im Label anzuzeigen meinetwegen 4 Sechsen =240 Punkte im Label oder 5 Vieren 200 Punkte.
Trotzdem Dank !!!
IngoD7
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 629


D7
BeitragVerfasst: Fr 21.05.04 22:21 
In dem Beispiel sind die Werte in der Prozedur WievielWerte greifbar - dort im Array nums[1..6]. Das Array ist da lokal und deshalb nur innerhalb der Prozedur greifbar.

Du kannst es dort ja z.B. in ein globales Array ablegen, auf welches du von überall her Zugriff hast.