Autor Beitrag
chriss1988
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Do 04.05.06 12:13 
hi und guten morgen

habe da ein kleines problem.
Das problem ist folgendermaßen:
Ich habe ein random, dort schreibt er mir in ein editfeld in meinem fall edit2 eine zahl zwischen 0-5 rein.
so nun drücke ich mehrmals auf den button, sodass immer eine andere zahl kommt.
Teilweise schreibt er mir dann 6 mal die selbe zahl hintereinander.
Nun möchte ich das er mir die zahlen nicht doppelt hintereinander ausgibt.
Lässt sich das machen oder muss ich damit leben??




PS:habe delphi5
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 04.05.06 12:27 
user profile iconchriss1988 hat folgendes geschrieben:
Lässt sich das machen oder muss ich damit leben??

Das lässt sich machen und du musst nicht damit leben.
Packe dein Random in eine Schleife (Repeat..until bietet sich an). Die Schleife muss solange laufen, bis das Ergebnis anders lautet, als der aktuelle Wert.
chriss1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Do 04.05.06 12:28 
???
ka was du da meinst sorry bin noch ein anfänger


so?

ausblenden Delphi-Quelltext
1:
2:
3:
repeat
zufallszahl:=random(6);     //zufallszahl 0-5
until BitBtn1;
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 04.05.06 12:43 
Da zumindest versucht hast, das umzusetzen, hier die Lösung:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
var
  zufallszahl : Integer;
begin
  repeat
    zufallszahl := Random(6);
  until zufallszahl <> StrToInt(Edit1.Text);
  Edit1.Text := IntToStr(zufallszahl);
end;

ungetester Code-Schnipsel.
chriss1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Do 04.05.06 12:53 
er bringt mir immer noch die zahlen doppelt wenn auch net so krass wie vorher.

jetzt ist noch ein fehler aufgetreten aber das is auch meine schuld hätte es erwänen sollen nämlich volgendermaßen.

ich mach wie ein zahlen rätsel.

ich geb in ein edit meine zahl ein drücke auf den button und dann vergleicht er meine zahl mit der zufalszahl wenn diese übereinstimmen bekomme ich einen punkt nun der fehler.
ich gebe die 4 ein nun bringt er mir alle zahlen bis 5 nur die 4 lässt er immer aus also die kommt nie bei 100 klicks kein einziges mal.
woran könnte das liegen?
chriss1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Do 04.05.06 12:59 
den 2 fehler habe ich gefunden
nun bringt er mir ne fehlermeldung '' ist kein gültiger gleitkommawert

so sieht mein code aus

ausblenden Delphi-Quelltext
1:
2:
3:
4:
repeat
    zufallszahl := Random(6);
  until zufallszahl <> strtofloat (edit2.text);
     edit2.text:=floattostr(zufallszahl);
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 04.05.06 13:03 
Wenn in deinem Edit noch nichts drin steht, kommt dieser Fehler.
Am einfachsten wäre es, wenn du das Edit mit "0" vorbelegst.
chriss1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Do 04.05.06 13:14 
so ich stell mal den code rein weil ich glaube man weis nicht was ich meine


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:
var
spielerzahl,spielerpunkte:single;
  pczahl,pcpunkte:single;
  zufallszahl:single;

procedure TEinzelspielerspiel.BitBtn1Click(Sender: TObject);
begin

randomize;
repeat
    zufallszahl := Random(6);
  until zufallszahl <> strtofloat(edit2.text);
     edit2.text:=floattostr(zufallszahl);



Spielerzahl:=strtofloat(edit1.text);

if Spielerzahl>5 then    //wenn zahl größer 5 ist
    begin
      showmessage('Deine Zahl ist größer als 5');
      edit1.clear;
      edit2.clear;
      edit3.clear;
      edit1.setfocus;
   end;


if Spielerzahl=zufallszahl then  //wenn Spielerzahl gleich zahl ist
   begin
     Spielerpunkte:=Spielerpunkte+1;   // bekommt er +1 Punkt
     label4.caption:=floattostr(Spielerpunkte);
   end
     else      //ansonsten

       if pczahl=zufallszahl then  //wenn pczahl gleich zahl ist
          begin
           Pcpunkte:=pcpunkte+1;  // bekommt er +1 Punkt
           label5.caption:=floattostr(pcpunkte);
          end;


if Spielerpunkte=5 then  //wenn spieler 5 punkte hat
   showmessage(panel1.caption+' hat gewonnen'); //hat er gewonnen
if Pcpunkte=5 then
showmessage('Pc hat gewonnen');
end;
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 04.05.06 13:26 
Heute habe ich meinen sozialen Tag:
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:
var
spielerzahl,spielerpunkte:single;
  pczahl,pcpunkte:single;
  zufallszahl:single;

procedure TEinzelspielerspiel.BitBtn1Click(Sender: TObject);
begin
  randomize;
  if edit2.text = '' then
    zufallszahl := Random(6)
  else
    repeat
      zufallszahl := Random(6);
    until zufallszahl <> strtofloat(edit2.text);

  edit2.text:=floattostr(zufallszahl);

  Spielerzahl:=strtofloat(edit1.text);

  if Spielerzahl>5 then    //wenn zahl größer 5 ist
  begin
    showmessage('Deine Zahl ist größer als 5');
    edit1.clear;
    edit2.clear;
    edit3.clear;
    edit1.setfocus;
  end;

  if Spielerzahl=zufallszahl then  //wenn Spielerzahl gleich zahl ist
  begin
     Spielerpunkte:=Spielerpunkte+1;   // bekommt er +1 Punkt
     label4.caption:=floattostr(Spielerpunkte);
  end
  else      //ansonsten
    if pczahl=zufallszahl then  //wenn pczahl gleich zahl ist
    begin
      Pcpunkte:=pcpunkte+1;  // bekommt er +1 Punkt
      label5.caption:=floattostr(pcpunkte);
    end;

  if Spielerpunkte=5 then  //wenn spieler 5 punkte hat
    showmessage(panel1.caption+' hat gewonnen'); //hat er gewonnen
  if Pcpunkte=5 then
    showmessage('Pc hat gewonnen');
end;

wie üblich: ungeprüft.
Bin mir aber nicht ganz sicher, ob es das ist, was du eigentlich wissen wolltest.
chriss1988 Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 389

windows xp prof,home,windows98
delphi5
BeitragVerfasst: Do 04.05.06 13:31 
danke für ddeinen sozialen tag;-)

ja es funktioniert musste noch ein wenig ändern aber jetzt passts

danke