Autor Beitrag
24karatboy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 110

Win XP
Delphi (Delphi Win32)
BeitragVerfasst: Di 12.06.07 09:52 
Hallo leute habe folgendes problem wollte ein programm schrieben wo man eine zahl eingeben kann wie oft gewürfelt werden soll und der pc würfelt dann und zählt bei jedem mal die gewürfelte zahl zu einer zahl dazu und soll sie hinterher ausgeben

benutze delphi 7

hier mal der code:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.wuerfelnClick(Sender: TObject);
var summe,teilsum,anzahl:real;
    i:integer;
begin
randomize;
i:=1;
anzahl:=strtofloat(anzahl.text);
while (i<=anzahl) do
begin
teilsum:=random(6)+1;
summe:=summe+teilsum;
end;


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


Zuletzt bearbeitet von 24karatboy am Di 12.06.07 09:58, insgesamt 1-mal bearbeitet
ZeitGeist87
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1593
Erhaltene Danke: 20

Win95-Win10
Delphi 10 Seattle, Rad Studio 2007, Delphi 7 Prof., C++, WSH, Turbo Pascal, PHP, Delphi X2
BeitragVerfasst: Di 12.06.07 10:01 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.wuerfelnClick(Sender: TObject);
var summe,teilsum,anzahl: Integer;
    i:integer;
begin
 randomize;

 summe:= 0;
 anzahl:=strtoint(anzahl.text);
 for i:= 1 to anzahl do
 begin
  teilsum:=random(6)+1;
  summe:=summe+teilsum;
 end;
 showmessage('Summe: ' + inttostr(summe));
end;


LG
Stefan

_________________
Wer Provokationen, Ironie, Sarkasmus oder Zynismus herauslesen kann soll sie ignorieren um den Inhalt meiner Beiträge ungetrübt erfassen zu können.
24karatboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 110

Win XP
Delphi (Delphi Win32)
BeitragVerfasst: Di 12.06.07 10:07 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.wuerfelnClick(Sender: TObject);
var summe,teilsum,anzahl,i:integer;
begin
randomize;
i:=1;
anzahl:=strtoint(anzahl.text);
while (i<=anzahl) do
begin
teilsum:=random(6)+1;
summe:=summe+teilsum;
end;
showmessage('Summe: ' + inttostr(summe));
end;

end.



Fehlermeldungen:

Erzeugen
[Fehler] Unit1.pas(40): Record, Objekt oder Klassentyp erforderlich
[Fehler] Unit1.pas(41): ';' erwartet, aber 'WHILE' gefunden
[Fehler] Unit1.pas(43): Undefinierter Bezeichner: 'teilsum'
[Fehler] Unit1.pas(44): Undefinierter Bezeichner: 'summe'
[Fehler] Unit1.pas(45): '.' erwartet, aber ';' gefunden
[Warnung] Unit1.pas(47): Text hinter dem abschließenden 'END.' wird vom Compiler ignoriert
[Fataler Fehler] Project1.dpr(5): Verwendete Unit 'Unit1.pas' kann nicht compiliert werden
rockminstrel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 38

Win XP Prof.
Delphi 5 Prof., Delphi 2005 pers., Turbo Delphi 2006 Exp.
BeitragVerfasst: Di 12.06.07 10:13 
Probiers mal so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
summe:=0;
teilsum:=0;
zahl:=0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
summe:=0;
zahl:=strtoint(anzahl.text);
for i:=0 to zahl do begin
    teilsum:= random(6)+1;
    summe:=summe+teilsum;
end;
label1.caption:=inttostr(summe);
end;

end.


Bei mir gehts.
Agawain
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 460

win xp
D5, MySQL, devxpress
BeitragVerfasst: Di 12.06.07 10:52 
Hi

Möcht ich mal bezweifeln, Daß das geht, weil die Schleife nie zum Ende kommt.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
for i:=0 to zahl do 
begin
    teilsum:= random(6)+1;
    summe:=summe+teilsum;
    i := i + 1;
end;


so wird da eher nen Schuh draus;)

und wo wir schon dabei sind, wenn teilsum keine weitere Funktion hat, dann auch so

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
for i:=0 to zahl do 
begin
      summe:=summe+random(6)+1;
      i := i + 1;
end;

//edit hab dias +1 wieder reingemacht, hab nicht bedacht, daß range 0 mit umfaßt;
rockminstrel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 38

Win XP Prof.
Delphi 5 Prof., Delphi 2005 pers., Turbo Delphi 2006 Exp.
BeitragVerfasst: Di 12.06.07 11:02 
Natürlich kommt die Schleife zum Ende. Eine Zeile darüber hab ich Zahl aus dem Edit ausgelesen. Das Teilsum hab ich nur übernommen, damit der Threadersteller seine Variablen wieder findet.

//edit wo ich gerade nochmal den thread anschau, seh ich, dass es sowieso nicht so wichtig ist, da ich etwas langsam mit meiner Lösung war
Leddl
Hält's aus hier
Beiträge: 2

Win XP
Delphi 2006 Prof.
BeitragVerfasst: Di 12.06.07 11:14 
user profile iconAgawain hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
for i:=0 to zahl do 
begin
      summe:=summe+random(6)+1;
      i := i + 1;
end;

:shock: Aua! Ein Erhöhen der Zählvariablen in einer For-Schleife :shock:
OlafSt
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 486
Erhaltene Danke: 99

Win7, Win81, Win10
Tokyo, VS2017
BeitragVerfasst: Di 12.06.07 11:26 
Leute, das erhöhen der Schleifenvariable in einer FOR-Schleife macht der Compiler !!!

_________________
Lies, was da steht. Denk dann drüber nach. Dann erst fragen.
24karatboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 110

Win XP
Delphi (Delphi Win32)
BeitragVerfasst: Di 12.06.07 11:27 
Titel: ...
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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    wuerfeln: TButton;
    beenden: TBitBtn;
    summeED: TEdit;
    anzahlED: TEdit;
    procedure beendenClick(Sender: TObject);
    procedure wuerfelnClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    randomize;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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

procedure TForm1.wuerfelnClick(Sender: TObject);
var summe,anzahl,i:integer;

begin
i:=1;
summe:=0;
anzahl:=0;

anzahl:=strtoint(anzahlED.text);
while (i<=anzahl) do
begin
summe:=summe+random(6)+1;
i=i+1;
end;
showmessage('Summe: ' + inttostr(summe));
end;

end.



Fehlermelungen:

Erzeugen
[Fehler] Unit1.pas(22): ',' oder ':' erwartet, aber ';' gefunden
[Fehler] Unit1.pas(23): Typ erwartet, aber 'END' gefunden
[Fehler] Unit1.pas(49): ':=' erwartet, aber '=' gefunden
[Fataler Fehler] Project1.dpr(5): Verwendete Unit 'Unit1.pas' kann nicht compiliert werden


habs mal ein wenig geändert aber naja immernoch fehler ...
rockminstrel
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 38

Win XP Prof.
Delphi 5 Prof., Delphi 2005 pers., Turbo Delphi 2006 Exp.
BeitragVerfasst: Di 12.06.07 11:43 
Das randomize im public muss ins formcreate oder in deine procedure. unten musst du bei dem Fehler in Zeile 49 i:=i+1 schreiben.


Zuletzt bearbeitet von rockminstrel am Di 12.06.07 11:46, insgesamt 1-mal bearbeitet
24karatboy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 110

Win XP
Delphi (Delphi Win32)
BeitragVerfasst: Di 12.06.07 11:44 
Titel: ..
danke euch allen funzt soweit ;)