Autor Beitrag
moppi
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Fr 17.11.06 23:11 
Hallo!
Ich hab ein kleines Problem. Und zwar muss ich für die Schule ein Programm mit Delphi 7 erstellen in dem man eine Zahl eingibt, die die Anzahl von Würfen wiedergibt. Dann soll der Computer eine Zufallszahl für die Häufigkeit der einzelnen Zahlen angeben.
Ich habe keine Ahnung und hab bisher nur mit dem Material aus dem Unterricht arbeiten können.
Ich weiß, dass ich eine FOR TO DO Schleife nehmen muss und wie ich eine Zufallszahl errechnen lassen kann.
Ich habe mal das letzte Stück meines Queltextes reingesetzt. Aber es kommen immer Fehler in der Declaration der Variablen...Kann mir jemand einen Hinweis geben, so dass ich weiterkomme?Danke!



ausblenden volle Höhe 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:
31:
32:
33:
procedure TForm1.Button1Click(Sender: TObject);
var a1,a2,a3,k,a4,a5,a6,i:real;

begin
a1:=0;a2:=0;a3:=0;a4:=0;a5:=0;a6:=0;
i:=1;
k:=StrToFloat(edit1.Text);

FOR i TO k Do
begin
x:=random(6)+1;
case x of
1:a1:=a1+1;
2:a2:=a2+1;
3:a3:=a3+1;
4:a4:=a4+1;
5:a5:=a5+1;
6:a6:=a6+1;
end;

edit2.Text:=FloatToStr(1);
edit3.Text:=FloatToStr(2);
edit4.Text:=FloatToStr(3);
edit5.Text:=FloatToStr(4);
edit6.Text:=FloatToStr(5);
edit7.Text:=FloatToStr(6);




end;

end.


Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 17.11.06 23:41 
ausblenden Delphi-Quelltext
1:
FOR i := 0 TO k Do					


1. Delphi-Tags benutzen!
2. Das verstehe ich nicht:
Zitat:

Dann soll der Computer eine Zufallszahl für die Häufigkeit der einzelnen Zahlen angeben.


Moderiert von user profile iconGausi: Delphi-Tags hinzugefügt :mrgreen:

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
moppi Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Fr 17.11.06 23:47 
Ich meine damit, dass er mir Zahlen angeben soll, wie oft die 1,2,3,4,5 und 6 dabei gewürfelt wurde...
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Fr 17.11.06 23:55 
Hallo,

case,for arbeitet nur mit ordinalen Typen ( Werte mit einer Ordnung Aufzaehlungstypen,integer,char und so weiter)
real, double, single,extended sind nicht abzaehlbar.
a1 bis a6,i müssen also vom typ integer sein. Wer oder was ist x ?
Also for i := 1 to k oder so. x deklarieren real,integer?? ....

Gruss Horst
moppi Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Sa 18.11.06 00:40 
hab ich jetzt rausgefunden.Program läuft auch,aber er gibt dann für 1,2,3,4,5,6 auch nur 1,2,3,4,5,6 aus...Also keine Zufallszahlen...???
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Sa 18.11.06 00:44 
Du bist ja auch ein witzbold:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
edit2.Text:=FloatToStr(1);
edit3.Text:=FloatToStr(2);
edit4.Text:=FloatToStr(3);
edit5.Text:=FloatToStr(4);
edit6.Text:=FloatToStr(5);
edit7.Text:=FloatToStr(6);


guck dir das doch mal an, was er da macht!

...

Tip: ich würds mitm Array machen, mit dem du deine Zufallszahlen festhälst!
Vergess randomize; im formcreate nicht!

grüße marc
moppi Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Sa 18.11.06 00:47 
Hab ich schon vorher alles in IntToStr verbessert...Oder was meinst du?
Hab echt nicht viel Ahnung davon.
Mein Quelltext sieht jetzt so aus:

ausblenden volle Höhe 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:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
procedure TForm1.Button1Click(Sender: TObject);
var a1,a2,a3,a4,a5,a6:integer;
var i,n,x:integer;


begin
n:=StrToInt(edit1.Text);
a1:=0;a2:=0;a3:=0;a4:=0;a5:=0;a6:=0;

FOR i:=1 TO n Do
begin   randomize;
x:=random(6)+1;

case x of

1:a1:=a1+1;
2:a2:=a2+1;
3:a3:=a3+1;
4:a4:=a4+1;
5:a5:=a5+1;
6:a6:=a6+1;
end;
edit2.Text:=IntToStr(1);
edit3.Text:=IntToStr(2);
edit4.Text:=IntToStr(3);
edit5.Text:=IntToStr(4);
edit6.Text:=IntToStr(5);
edit7.Text:=IntToStr(6);

end;



end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

end.


Moderiert von user profile iconGausi: Delphi-Tags hinzugefügt
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Sa 18.11.06 00:52 
Am besten schläfst du dich mal aus und denkst mit klarem Kopf nochmal darüber nach^^.
Na also
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
 edit2.Text:=IntToStr(1);
 edit3.Text:=IntToStr(2);
 edit4.Text:=IntToStr(3);
 edit5.Text:=IntToStr(4);
 edit6.Text:=IntToStr(5);
 edit7.Text:=IntToStr(6);

In diesem Falle stehen natürlich keine Zufallszahlen in deinen Edits. Warum wohl!?


Zuletzt bearbeitet von Marc. am Sa 18.11.06 00:55, insgesamt 1-mal bearbeitet
rizla
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: Sa 18.11.06 00:54 
hi!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
edit2.Text:=IntToStr(1);
edit3.Text:=IntToStr(2);
edit4.Text:=IntToStr(3);
edit5.Text:=IntToStr(4);
edit6.Text:=IntToStr(5);
edit7.Text:=IntToStr(6);


in
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
edit2.Text:=IntToStr(a1);
edit3.Text:=IntToStr(a2);
edit4.Text:=IntToStr(a3);
edit5.Text:=IntToStr(a4);
edit6.Text:=IntToStr(a5);
edit7.Text:=IntToStr(a6);


ändern!

:r!a:

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
moppi Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Sa 18.11.06 00:58 
@rizla: Vielen Dank das funktioniert so...

Natürlich auch den anderen vielen Dank!(aber das war einfach zu kompliziert für mich)

Jetzt kann ich in Ruhe schlafen. ;-)
rizla
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: Sa 18.11.06 01:10 
habs noch mal gekürzt, meine form hat n button und ne listbox, einziger code (very very rude):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm1.Button1Click(Sender: TObject);
var i,rnd: integer;    // variablen
rr: array[1..6of integer;  // array für die 6 kandidaten, aber real braucht man net
begin
randomize;      // randomizer anschmeissen ;)
 for i := 1 to 6 do rr[i] := 0;  // array nivelieren, sprich alle auf 0 resetten
 for i := 1 to 10 do    // einfach mal 10 zahlen, bei moppi halt inttostr(edit1..)
  begin
    rnd := random(6)+1;    // zufallszahl
    inc(rr[rnd]);    // kürzer gehts wohl net
  end;
 for i := 1 to 6 do    // ausgeben der 6 zahlen + anzahl dr "ziehungen"
  listbox1.Items.Add(inttostr(i)+': '+ inttostr(rr[i]));
end;


:r!a:

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10184
Erhaltene Danke: 1259

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 18.11.06 01:19 
Moin!

Zum Thema "kürzer geht´s nicht": ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.Button1Click(Sender: TObject);
  var
    i: Integer;
    Cnt: array[1..6of Integer;
begin
  Randomize; // Zufallszahlengenerator initialisieren
  ZeroMemory(@Cnt,SizeOf(Cnt)); // Array nivelieren, sprich alle auf 0 setzen
  for i := 1 to StrToIntDef(Edit1.Text,1do // Anzahl aus Edit1 lesen
    Inc(Cnt[Succ(Random(6))]); // kürzer gehts wohl net ;)
  ListBox1.Clear;
  for i := 1 to 6 do // Ausgeben der 6 Zahlen + Anzahl der "Ziehungen"
    ListBox1.Items.Add(IntToStr(i)+': '+ IntToStr(Cnt[i]));
end;

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
rizla
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: So 19.11.06 20:06 
1:0 für narses ;)

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
Dragonclaw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: Di 21.11.06 20:59 
Kürzer ja, aber leider falsch... :)
ausblenden Delphi-Quelltext
1:
2:
3:
procedure TForm1.Button1Click(Sender: TObject);
  for i := 1 to StrToIntDef(Edit1.Text,1do // Anzahl aus Edit1 lesen
    Inc(Cnt[Succ(Random(6+1))]); // kürzer gehts wohl net ;)



mhmmm, wusste garnicht das man mit Succ() den Bereich verschieben kann :)
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10184
Erhaltene Danke: 1259

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 21.11.06 23:05 
Moin und :welcome: im Forum @user profile iconDragonclaw!

user profile iconDragonclaw hat folgendes geschrieben:
aber leider falsch... :)

Allerdings... :mrgreen:

ausblenden Delphi-Quelltext
1:
Random(6+1)					

Das liefert dann Zahlen im Bereich von 0..6 :shock: Spannend, was du so für Würfel hast... ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Dragonclaw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: Di 21.11.06 23:16 
:D , da mein ich nen Fehler gefunden zu haben und korrigier ihn auch noch falsch. Muss natürlich (random(5)+1) sein.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10184
Erhaltene Danke: 1259

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 21.11.06 23:17 
Moin!

Und genau das steht da: Succ(Random(6)) :mrgreen:

Ich empfehle mal die DOH zu Succ(). ;)

//EDIT: Jetzt ist es übrigens wieder falsch in deinem Beitrag (haste wohl gerade editiert). :? Random(5)+1 -> 1..5!

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Dragonclaw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: Di 21.11.06 23:33 
Mhmm ich versteht das mit Succ() nicht wirklich...

Laut Delphi
Zitat:

In Delphi gibt Succ den Nachfolger des Arguments X zurück.

X ist ein Ausdruck mit einem ordinalen Datentyp (einschließlich Int64). Der zurückgegebene Wert ist der Nachfolger von X und hat denselben Datentyp.


Aber ist das nicht das Selbe was Inc() macht?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10184
Erhaltene Danke: 1259

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 21.11.06 23:35 
Moin!

An dieser Stelle ist das fast gleich, nur ist Inc() eine Prozedur und Succ() eine Funktion. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.