Autor Beitrag
Stonebreaker
Hält's aus hier
Beiträge: 13



BeitragVerfasst: Do 17.07.03 20:43 
hi Leutz!

ja, also wie gesgat brauche ich eine ganze Zufallszahl zwischen 1 und 15 (heißt: ich brauche die Zahlen 1 und 15 auch, muss ich dann zwischen 0 und 16 nehmen?)! Ich habe mir die Delphi-Hilfe angeschaut und dann so etwas gemacht (Ich hab das Beispiel jetzt nicht mit 15 verschieden Werten gemacht):

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:
28:
procedure TForm1.Button1Click(Sender: TObject);
 var
  zahl:Integer;
 begin
  Randomize ;
    for zahl := 1 to 6 do
    begin

     if zahl = 1
     then label1.caption := 'hallo';

     if zahl = 2
     then label1.caption := 'moin';

     if zahl = 3
     then label1.caption := 'tach';

     if zahl = 4
     then label1.caption := 'ao';

     if zahl = 5
     then label1.caption := 'guten tag';

     if zahl = 6
     then label1.caption := grüß Gott';
     end;
 end;
end.

Da da immer die selbe Zahl bei rum kommt, hab ich mich auf Delphi-source umgesehen und das hier gefunden: bite hier klicken

da kapier ich allerdings nicht, was die Zahl in er Klammer zu bedeuten hat, da ich ja eine ganzeZufallszahl zwischen 1 und 15 brauche!!!

Ich hoffe ihr könnt und werdet mir helfen! Danke schon mal im voraus fürs lesen,

Stonie

Moderiert von user profile iconTino: Delphi-Tags hinzugefügt.

_________________
life is too short --> skate faster
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Do 17.07.03 21:02 
Hallo

nimm das randomize raus aus der buttonclick-procedure. randomize sollte nur einmal aufgerufen werden. ein guter Platz ist oncreate der Form.

du rufst random überhaupt nicht auf. random liefert die Zufalszahlen von 0 bis angegebener Wert-1

in deinem Fall wäre das
ausblenden Delphi-Quelltext
1:
zahl:=random(15)+1;					

random (15) liefert würd dir 0.. 14 zurückgeben und noch 1 dazuaddieren bist du bei 1..15 . alles klar?
ausblenden Delphi-Quelltext
1:
2:
3:
    if zahl = 1
     then label1.caption := 'hallo'
...

gugg dir case an
ausblenden Delphi-Quelltext
1:
2:
3:
4:
case zahl of 
  1label.caption := 'hallo';
  2label.caption := 'moin';
 else end//of case

mfg frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Stonebreaker Threadstarter
Hält's aus hier
Beiträge: 13



BeitragVerfasst: Do 17.07.03 21:12 
danke, das mit dem case schau ich mir mal an! Aber ich will doch erst eine Zufallszahl bekommen, wenn ich auf den Button klicken, und wo bitte rufe ich das 2mal auf?

soll ich das so schreiben?:
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:
procedure TForm1.Button1Click(Sender: TObject); 
var 
  zahl:Integer; 
begin  
    zahl := random(15)+1;
    

     if zahl = 1 
     then label1.caption := 'hallo'

     if zahl = 2 
     then label1.caption := 'moin'

     if zahl = 3 
     then label1.caption := 'tach'

     if zahl = 4 
     then label1.caption := 'ao'

     if zahl = 5 
     then label1.caption := 'guten tag'

     if zahl = 6 
     then label1.caption := grüß Gott'; 
end; 
end.


mfg, Stonie

_________________
life is too short --> skate faster
derDoc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Do 17.07.03 21:30 
Mach das doch 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:
22:
23:
24:
25:
26:
27:
28:
procedure TForm1.Button1Click(Sender: TObject); 
var
  zahl: Integer;
begin
  zahl := random(16); //von 1 bis 15
  case zahl of
    1: ; //hier kommen irgendwelche Anweisungen rein
    2: ;
    3: ;
    4: ;
    5: ;
    6: ;
    7: ;
    8: ;
    9: ;
    10: ;
    11: ;
    12: ;
    13: ;
    14: ;
    15: ;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
end;

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Do 17.07.03 21:39 
derDoc hat folgendes geschrieben:

zahl := random(16); //von 1 bis 15

sicher ? :wink:

ich hab nicht ohne grund geschrieben
Keldorn hat folgendes geschrieben:

in deinem Fall wäre das
ausblenden Quelltext
1:
zahl:=random(15)+1;					

random (15) würd dir 0.. 14 zurückgeben und noch 1 dazuaddieren bist du bei 1..15 . alles klar?


Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Stonebreaker Threadstarter
Hält's aus hier
Beiträge: 13



BeitragVerfasst: Fr 18.07.03 11:51 
okay, danke!
Es funzt jetzt auf jeden Fall!!!
Hab mich verzählt und jetzt 16 Werte genommen und es mit "random(16)+1" gemacht. Wie gesacht so klappt es!!!

mfg, Stonie

_________________
life is too short --> skate faster
derDoc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Fr 18.07.03 12:43 
@Keldorn:
Du hast recht
ausblenden Delphi-Quelltext
1:
Random(16)					

gibt von 0 bis 15 zurück.

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
Nightmare_82
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 260



BeitragVerfasst: Di 22.07.03 17:30 
ich würd an deiner Stelle einen Array mit den Strings anlegen, dann gehts leichter zu programmieren:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
const ITEMS = 15;
const MyArray : array[0..ITEMS-1of String = ('Hallo',.....)
...
procedure TForm1.Button1Click(Sender: TObject);  
var 
  zahl: Integer; 
begin 
  zahl := random(ITEMS)+1//von 1 bis 15
  label1.caption:= MyArray[zahl-1];// da der Array bei 0 anfängt
end;