Autor Beitrag
Abu36
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Mo 21.03.05 19:24 
Ich muss für den informatikunterricht ein programm machen: Mit vier Würfeln gleichzeitig würfeln und immer wenn eine sechs gewürfelt wird verdoppelt sich der zuvor eingegebene einsatz, wenn bei den vier würfeln keine sechs dabei ist behält die spielbank das geld.
die würfel würfeln zulassen bekomm ich hin; mein problem ist, wie ich den gewinn berechen soll wenn z.B. drei sechsen gewüefelt werden -> ermüsste sich also dreimal verdoppeln (bei 5 € einsatz dann also 40 € gewinn).

Mein prgramm

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:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    StringGrid1: TStringGrid;
    Label7: TLabel;
    Edit2: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i:Integer;
begin
 for i:= 1 to 6 do
     StringGrid1.Cells[i,0]:= inttostr(i);
     StringGrid1.Cells[0,0]:='Augenzahl';
     StringGrid1.Cells[0,1]:='Dein Wurf';
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var w1, j, k, s, g, e: Integer;
    a:array [1..6]of integer;
    r:array [1..6]of real;
begin
 randomize;
  for j:=1 to 6 do a[j]:=0;
  for k:=1 to 4 do
   begin
     w1:=random(6)+1;
     a[w1]:=a[w1]+1;
   end;
  for k:=1 to 6 do
   begin
     stringgrid1.Cells[k,1]:=inttostr(a[k]);
     r[j]:=a[j]/6;
  end;
  e:=strtoint(edit1.Text);
end;
end.



Nachdem ich den einsatz eigelesen habe brauche ich jetzt noch die formel für die berechnung des gewinns.
würde mich freuen wenn jemand antwortet.

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 22.03.05 00:40 
Die "Formel" geht genau so, wie du es beschrieben hast:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
if AnzahlSechser >= 1 then
 Gewinn := Einsatz*Power(2,AnzahlSechser)
else
 Gewinn := -Einsatz;
Abu36 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Mi 30.03.05 15:28 
Ich bin delphi anfänger könnt ihr das auch für mich verständlich aufschreiben
OneOfTen
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 30.03.05 15:43 
Bist du auch Mathe-Anfänger?

Power(2, AnzahlSechser) = 2^AnzahlSechser

also nichts anderes als eine 2er Potenz multipliziert mit deinem Einsatz
Abu36 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 31.03.05 13:23 
Hab jetzt alles gemacht, es wird auch richtig ausgerechnet, nur ein Fehler tritt auf: In meinem Stringgrid-Feld wird in der Spalte der Augenzahl 2 immer ein und der Selbe falsche Wert angezeigt, welcher lautet: 1092970075.
Die anderen werte Stimmen alle.




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:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    StringGrid1: TStringGrid;
    Label7: TLabel;
    Edit2: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i:Integer;
begin
 for i:= 1 to 6 do
     StringGrid1.Cells[i,0]:= inttostr(i);
     StringGrid1.Cells[0,0]:='Augenzahl';
     StringGrid1.Cells[0,1]:='Dein Wurf';
end;



procedure TForm1.BitBtn1Click(Sender: TObject);
var w1, j, k, s, g, e: Integer;
    a:array [1..6]of integer;
    r:array [1..6]of real;
begin

 randomize;
  for j:=1 to 6 do a[j]:=0;
  for k:=1 to 4 do
   begin
     w1:=random(6)+1;
     a[w1]:=a[w1]+1;
   end;

  for k:=1 to 6 do
   begin
     stringgrid1.Cells[k,1]:=inttostr(a[k]);
     r[j]:=a[j]/6;
  end;

  e:=strtoint(edit1.Text);
  s:=strtoint(stringgrid1.cells[6,1]);

  if s=0 then
    g:=-e
  else if s=1 then
    g:=e*2
  else if s=2 then
    g:=e*4
  else if s=3 then
    g:=e*8
  else if s=4 then
    g:=e*16;

  edit2.Text:=inttostr(g);

end;


end.


Moderiert von user profile iconAXMD: Delphi-Tags hinzugefügt.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Do 31.03.05 13:31 
Du solltest deinen Quelltext etwas mehr kommentieren; ich würde dir gerne helfen - nur hab ich echte Probleme, deinen Code ohne Kommentare zu verstehen

AXMD
Abu36 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 31.03.05 14:14 
Also die labels sind alle uninteressant(nur eine spielerklärung)
Im edit1 wird einsatz eingegeben
edit2 gibt gewinn aus
Zwei BitBtn (Beenden und Wiederholen)
Stringgrid1 feld zeigt die gewürfelten zahlen

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.FormCreate(Sender: TObject);  
var i:Integer;  
begin  
 for i:= 1 to 6 do  
     StringGrid1.Cells[i,0]:= inttostr(i);  
     StringGrid1.Cells[0,0]:='Augenzahl';  
     StringGrid1.Cells[0,1]:='Dein Wurf';  
end;


-> beschriftet stringgridfeld


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:
procedure TForm1.BitBtn1Click(Sender: TObject);  
var w1, j, k, s, g, e: Integer;  
    a:array [1..6]of integer;  
    r:array [1..6]of real;  
begin  
 randomize;  
  for j:=1 to 6 do a[j]:=0;  
  for k:=1 to 4 do  
   begin  
     w1:=random(6)+1;  
     a[w1]:=a[w1]+1;  end;  
  for k:=1 to 6 do  begin  
     stringgrid1.Cells[k,1]:=inttostr(a[k]);  
     r[j]:=a[j]/6;  end;  
  e:=strtoint(edit1.Text);                     -> lese einsatz ein
  s:=strtoint(stringgrid1.cells[6,1]);         -> anzahl der sechsen bestimmen oder ablesen  
  if s=0 then  
    g:=-e                                      -> keine sechs = minus einsatz  
  else if s=1 then  
    g:=e*2                                     -> eine sechs -> einsatz einmal verdoppeln(mal 2)
  else if s=2 then  
    g:=e*4  
  else if s=3 then  
    g:=e*8  
  else if s=4 then  
    g:=e*16;                                   -> 4 sechsen (sehr unwahrscheinlich)= einsatz*16
edit2.Text:=inttostr(g);  end;  end.              oder einsatz * (2 hoch Anzahl sechsen)



-> w1= würfel -> es wird vier mal gewürfelt
s = ist die anzahl der Sechsen
e = ist der einsatz
genau spielbeschreibung steht im ersten beitrag

als ich noch nicht die gewinnberechnung hatte, also nur das würfeln, gab es keinen fehler

Moderiert von user profile iconAXMD: Delphi-Tags hinzugefügt.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Do 31.03.05 14:21 
Eigentlich meinte ich Kommentare so wie sie in Delphi üblich sind ({so} //oder so). Außerdem bitte das nächste Mal delphi-Tags verwenden.

Danke :)
AXMD
Abu36 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 31.03.05 14:31 
ich weiss nicht wie du das meinst
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Do 31.03.05 14:36 
user profile iconAbu36 hat folgendes geschrieben:
ich weiss nicht wie du das meinst


Zu den Delphitags findest du Informationen in der Hilfe, das mit den Kommentaren meinte ich so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer; //Schleifenvariable
begin
  for i := 1 to 10 do
    ShowMessage(IntToStr(i)); //Die Zahlen von 1 bis 10 ausgeben
end;


So in etwa.

AXMD
MitschL
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 211

Win 98 SE, Win 2000
D5 Pers, D6 Pers und D7 Pro
BeitragVerfasst: Do 31.03.05 14:52 
Da wo bei dir im Code -> für deine Kommentare steht, sollte stattdessen // hin (siehe unten).
Du kannst meist auf Kommentare verzichten, wenn deine Variablen lesbare Namen hätten. Unter der variable wuerfel_ergebnisse kann ich mir mehr vorstellen, als unter a, wl oder sonstwas.

Ich hab mal kurz geschaut und möchte meinen, daß mir folgendes auffiel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
//...
  for j:=1 to 6 do a[j]:=0;     
  for k:=1 to 4 do
  begin     
     w1:=random(6)+1;     
     a[w1]:=a[w1]+1;  end;     
  for k:=1 to 6 do  begin     
     stringgrid1.Cells[k,1]:=inttostr(a[k]);     
     r[j]:=a[j]/6;  end;     
  e:=strtoint(edit1.Text);                     // lese einsatz ein   
//...

In einer k-Schleife, das j einer weiter oben genutzten Schleife benutzen?

Bei den Variablen ist es aber nicht sehr toll, sich da durchzuarbeiten.

gegrüßt!

[edit] Für dein if-then-else-if-Konstrukt, kannst du das Code-Schnipselchen von delfiphan so ziemlich direkt nutzen.

_________________
"Bloßes Ignorieren ist noch keine Toleranz." (Theodor Fontane)
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Do 31.03.05 17:22 
user profile icondelfiphan hat folgendes geschrieben:
Die "Formel" geht genau so, wie du es beschrieben hast:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
if AnzahlSechser >= 1 then
 Gewinn := Einsatz*Power(2,AnzahlSechser)
else
 Gewinn := -Einsatz;

Kurzer Nachtrag: Wie ich sehe arbeitest du bei der Berechnung des Gewinns mit Integer. Ich ging davon aus, dass man auch halbe Euro setzen kann, deswegen ist beim obigen Sourcecode Gewinn eine Gleitkommazahl. Um mein Codeschnipsel mit deinem Programm kompatibel zu machen müsstest du also das Resultat von Power noch mit round runden.