| Autor |
Beitrag |
moppi
Hält's aus hier
Beiträge: 5
|
Verfasst: 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!
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 Christian S.: Delphi-Tags hinzugefügt
|
|
Marco D.
      
Beiträge: 2750
Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
|
Verfasst: Fr 17.11.06 23:41
_________________ 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 
Hält's aus hier
Beiträge: 5
|
Verfasst: 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
      
Beiträge: 1654
Erhaltene Danke: 244
WIN10,PuppyLinux
FreePascal,Lazarus
|
Verfasst: 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 
Hält's aus hier
Beiträge: 5
|
Verfasst: 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.
      
Beiträge: 1876
Erhaltene Danke: 129
Win 8.1, Xubuntu 15.10
|
Verfasst: Sa 18.11.06 00:44
Du bist ja auch ein witzbold:
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 
Hält's aus hier
Beiträge: 5
|
Verfasst: 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:
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 Gausi: Delphi-Tags hinzugefügt
|
|
Marc.
      
Beiträge: 1876
Erhaltene Danke: 129
Win 8.1, Xubuntu 15.10
|
Verfasst: 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
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
      
Beiträge: 417
Erhaltene Danke: 2
XP
FPC mit Lazarus
|
Verfasst: Sa 18.11.06 00:54
hi!
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
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 
Hält's aus hier
Beiträge: 5
|
Verfasst: 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
      
Beiträge: 417
Erhaltene Danke: 2
XP
FPC mit Lazarus
|
Verfasst: Sa 18.11.06 01:10
habs noch mal gekürzt, meine form hat n button und ne listbox, einziger code (very very rude):
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; rr: array[1..6] of integer; begin randomize; for i := 1 to 6 do rr[i] := 0; for i := 1 to 10 do begin rnd := random(6)+1; inc(rr[rnd]); end; for i := 1 to 6 do 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
      

Beiträge: 10184
Erhaltene Danke: 1259
W11x64
TP3 .. D7pro .. D10.2CE
|
Verfasst: Sa 18.11.06 01:19
Moin!
Zum Thema "kürzer geht´s nicht":
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..6] of Integer; begin Randomize; ZeroMemory(@Cnt,SizeOf(Cnt)); for i := 1 to StrToIntDef(Edit1.Text,1) do Inc(Cnt[Succ(Random(6))]); ListBox1.Clear; for i := 1 to 6 do 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
      
Beiträge: 417
Erhaltene Danke: 2
XP
FPC mit Lazarus
|
Verfasst: 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
      
Beiträge: 196
Windows Vista
Delphi 7 Prof.
|
Verfasst: Di 21.11.06 20:59
|
|
Narses
      

Beiträge: 10184
Erhaltene Danke: 1259
W11x64
TP3 .. D7pro .. D10.2CE
|
Verfasst: Di 21.11.06 23:05
Moin und  im Forum @ Dragonclaw!
Dragonclaw hat folgendes geschrieben: | aber leider falsch...  |
Allerdings...
Delphi-Quelltext
Das liefert dann Zahlen im Bereich von 0..6  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
      
Beiträge: 196
Windows Vista
Delphi 7 Prof.
|
Verfasst: Di 21.11.06 23:16
 , da mein ich nen Fehler gefunden zu haben und korrigier ihn auch noch falsch. Muss natürlich (random(5)+1) sein.
|
|
Narses
      

Beiträge: 10184
Erhaltene Danke: 1259
W11x64
TP3 .. D7pro .. D10.2CE
|
Verfasst: Di 21.11.06 23:17
Moin!
Und genau das steht da: Succ(Random(6))
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
      
Beiträge: 196
Windows Vista
Delphi 7 Prof.
|
Verfasst: 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
      

Beiträge: 10184
Erhaltene Danke: 1259
W11x64
TP3 .. D7pro .. D10.2CE
|
Verfasst: 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.
|
|