Lotto programm
Hallo Leute habe jetzt ein Programm das mir aus 45 zahlen 6 zufällige nimmt.
Delphi-Quellcode:
Und ich habe folgendes Proplem. Habe jetzt 6 weitere Edits gemacht und möchte das wenn man unten zahlen rein schreibt das delphi die geschriebenen zahlen mit den zufalls zahlen in den anderen edits vergleicht und wenn alle übereinstimmen soll er mitels showmessage sagen das man gewonnen hat und wenn nicht das man verloren hat. :dp:
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var a,b,c,d,e,f :integer; begin randomize; repeat a:= random (46); b:= random (46); c:= random (46); d:= random (46); e:= random (46); f:= random (46); until (a<>b) and (a<>c) and (a<>d) and (a<>e) and (a<>f) and (b<>a) and (b<>c) and (b<>d) and (b<>e) and (b<>f) and (c<>a) and (c<>b) and (c<>d) and (c<>d) and (c<>f) and (d<>a) and (d<>b) and (d<>c) and (d<>e) and (d<>f) and (e<>a) and (e<>b) and (e<>c) and (e<>d) and (e<>f) and (f<>a) and (f<>b) and (f<>c) and (f<>d) and (f<>e); edit1.Text := inttostr (a+1); edit2.Text := inttostr (b+1); edit3.Text := inttostr (c+1); edit4.Text := inttostr (d+1); edit5.Text := inttostr (e+1); edit6.Text := inttostr (f+1); end; procedure TForm1.Button2Click(Sender: TObject); begin close; end; procedure TForm1.Button3Click(Sender: TObject); begin edit1.clear; edit2.clear; edit3.clear; edit4.clear; edit5.clear; edit6.clear; end; end. Das Haupt proplem ist ich weiß nciht wie ich es schreiben soll das er die zahlen überprüft also vergleicht. |
Re: Lotto programm
Die EIngegebenen Werte in Integer umwandeln kannst du mit MyInt:=StrToInt(String);
Vegleichen solltest du können. Aber du wirst diese Meldung praktisch niemals zu gesicht bekommen, weil die Wahrscheinlichkeit dass du die richtigen Zahlen bekommst verschwinded gering ist ;) btw: müssten es nicht 6 aus 49 sein? |
Re: Lotto programm
die wahrscheinlichkeit ist zwar gering aber es ist eben lotto nein es sollen 45 zahlen sein und auserdem kann ich das vergleichen net :?:
|
Re: Lotto programm
Was kannst du nicht vergleichen?
|
Re: Lotto programm
die zahlen der Computer soll die geschriebennen zahlen mit den zufalls zahlen vergleichen udn dasnn sagen wenn alle übereinstimmen SIe haben gewoonen ansonsten sie haben verloren
|
Re: Lotto programm
Warum scheiterst du am Vergleichen. Wo liegt das konkrete Problem? Was hast du bisher.
Noch 2 Tipps am Rande:
Um zu prüfen, wieviele Richtige du hast würde ich mal versuchen mit
Delphi-Quellcode:
zu arbeiten, damit sollte eine Prüfung relativ leicht werden, wieviele Richtige du hast.
set of integer ;
|
Re: Lotto programm
Zitat:
Das Haupt proplem ist ich weiß nciht wie ich es schreiben soll das er die zahlen überprüft also vergleicht |
Re: Lotto programm
Delphi-Quellcode:
besser aber TryStrToInt oder Val verwenden.
if StrToInt( Edit1.text) = StrToInt( Edit2.text) then ...
|
Re: Lotto programm
Hallo,
mach Dir doch drei Arrays
Delphi-Quellcode:
gezogeneZahlen : Array[1..6] of Byte;
gewaehlteZahlen: Array[1..6] of Byte; ZahlenRichtig : Array[1..6] of Byte;
Delphi-Quellcode:
Wenn dann am Ende k = 6 ist, dann stimmen 6 Zahlen überein.
k:=0;
for i:=1 to 6 do for j:=1 to 6 do if gewaehlteZahlen[i] = gezogeneZahlen[j] then begin inc(k); ZahlenRichtig[k]:=gewaehlteZahlen[j]; end; Wenn es weniger sind entspricht k der Anzahl der richtigen Zahlen. Die richtigen Zahlen stehen dann im Array ZahlenRichtig. Grüße Klaus |
Re: Lotto programm
Klaus01 habe schon eine variante
Aber ähm wie schreibe ich wenn edit1.text nicht gleich edit2.text ist dann false Denke mir if Stroint (edit1.text) (was gibt man als unglecih ein) stroint (edit2.text) than .... Das Proplem ist ich weiß nicht wie amn ungleich schreibt in dwelphi. |
Re: Lotto programm
Ungleich: <>
|
Re: Lotto programm
ok danke
|
Re: Lotto programm
Hab soweit alles aber er zeigt mir folgenden fehler an
[Pascal Fehler] Unit1.pas(69): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Boolean' gefunden [Pascal Fehler] Unit1.pas(70): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Boolean' gefunden [Pascal Fehler] Unit1.pas(71): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Boolean' gefunden [Pascal Fehler] Unit1.pas(72): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Boolean' gefunden [Pascal Fehler] Unit1.pas(73): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Boolean' gefunden [Pascal Fehler] Unit1.pas(74): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Boolean' gefunden [Pascal Fehler] Unit1.pas(78): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Boolean' gefunden
Delphi-Quellcode:
if StrToInt( edit1.text) = StrToInt( edit7.text) then true ;
if StrToInt( Edit2.text) = StrToInt( Edit8.text) then true ; if StrToInt( Edit3.text) = StrToInt( Edit9.text) then true ; if StrToInt( Edit4.text) = StrToInt( Edit10.text) then true ; if StrToInt( Edit5.text) = StrToInt( Edit11.text) then true ; if StrToInt( Edit6.text) = StrToInt( Edit12.text) then true ; ToInt( Edit1.text) <> StrToInt( Edit7.text) then false; if StrToInt( Edit2.text) <> StrToInt( Edit8.text) then false; if StrToInt( Edit3.text) <> StrToInt( Edit9.text) then false; if StrToInt( Edit4.text) <> StrToInt( Edit10.text) then false; if StrToInt( Edit5.text) <> StrToInt( Edit11.text) then false; if StrToInt( Edit6.text) <> StrToInt( Edit12.text) then false |
Re: Lotto programm
E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Boolean' gefunden
Meint : if StrToInt( Edit2.text) <> StrToInt( Edit8.text) then false; Was ist false? Du musst das false in eine Variable schreiben oder es weglassen. Btw. hab ich keine Ahnung was du da machst ^^ |
Re: Lotto programm
if false then showmessage ('Sie haben leider verloren');
|
Re: Lotto programm
Delphi-Quellcode:
var
... falsch: boolean; ... if StrToInt( Edit4.text) = StrToInt( Edit10.text) then falsch := true ; |
Re: Lotto programm
was soll heißen falsch := true was bringt sich das kann ich net einfach false und true als boolena eingeben also
var false:boolean; true: boolean; |
Re: Lotto programm
false und true sind Konstanten.
|
Re: Lotto programm
ja und weiter
|
Re: Lotto programm
das sind reservierte Wörter. Du musst eine Variable vom Typ Boolean nutzen und ihr dann false bzw. true zuweisen.
(so, wie es mkinzler geschrieben hat) |
Re: Lotto programm
sol cih einfach richtig : boolean dann
if StrToInt( edit1.text) = StrToInt( edit7.text) then richtig:= true ; |
Re: Lotto programm
genau so.
|
Re: Lotto programm
aber jetzt steht sie haben gewoonen auch da wenn die zahlen nicht gleich sind
|
Re: Lotto programm
Hier nochmal der ganze code bis jetzt
Delphi-Quellcode:
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Button1: TButton; Button2: TButton; Button3: TButton; Edit7: TEdit; Edit8: TEdit; Edit9: TEdit; Edit10: TEdit; Edit11: TEdit; Edit12: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var a,b,c,d,e,f :integer; richtig: boolean; falsch: boolean; begin randomize; repeat a:= random (46); b:= random (46); c:= random (46); d:= random (46); e:= random (46); f:= random (46); until (a<>b) and (a<>c) and (a<>d) and (a<>e) and (a<>f) and (b<>a) and (b<>c) and (b<>d) and (b<>e) and (b<>f) and (c<>a) and (c<>b) and (c<>d) and (c<>d) and (c<>f) and (d<>a) and (d<>b) and (d<>c) and (d<>e) and (d<>f) and (e<>a) and (e<>b) and (e<>c) and (e<>d) and (e<>f) and (f<>a) and (f<>b) and (f<>c) and (f<>d) and (f<>e); edit1.Text := inttostr (a+1); edit2.Text := inttostr (b+1); edit3.Text := inttostr (c+1); edit4.Text := inttostr (d+1); edit5.Text := inttostr (e+1); edit6.Text := inttostr (f+1); if StrToInt( edit1.text) = StrToInt( edit7.text) then richtig := true ; if StrToInt( Edit2.text) = StrToInt( Edit8.text) then richtig := true ; if StrToInt( Edit3.text) = StrToInt( Edit9.text) then richtig := true ; if StrToInt( Edit4.text) = StrToInt( Edit10.text) then richtig := true ; if StrToInt( Edit5.text) = StrToInt( Edit11.text) then richtig := true ; if StrToInt( Edit6.text) = StrToInt( Edit12.text) then richtig := true ; if true then showmessage ('Sie haben Gewonnen'); if StrToInt( Edit1.text) <> StrToInt( Edit7.text) then falsch := false ; if StrToInt( Edit2.text) <> StrToInt( Edit8.text) then falsch := false ; if StrToInt( Edit3.text) <> StrToInt( Edit9.text) then falsch := false ; if StrToInt( Edit4.text) <> StrToInt( Edit10.text) then falsch := false ; if StrToInt( Edit5.text) <> StrToInt( Edit11.text) then falsch := false ; if StrToInt( Edit6.text) <> StrToInt( Edit12.text) then falsch := false ; if false then showmessage ('Sie haben leider verloren'); end; procedure TForm1.Button2Click(Sender: TObject); begin close; end; procedure TForm1.Button3Click(Sender: TObject); begin edit1.clear; edit2.clear; edit3.clear; edit4.clear; edit5.clear; edit6.clear; edit7.clear; edit8.clear; edit9.clear; edit10.clear; edit11.clear; edit12.clear; end; end. |
Re: Lotto programm
Nicht if true bzw. if false, sondern if richtig/if falsch
|
Re: Lotto programm
Delphi-Quellcode:
Wenn die Variable "falsch" wahr sein soll, muss sie auf True gesetzt werden.
if StrToInt( Edit1.text) <> StrToInt( Edit7.text) then falsch := TRUE;
Und dann noch:
Delphi-Quellcode:
if richtig then showmessage ('Sie haben Gewonnen');
... if falsch then showmessage ('Sie haben leider verloren'); |
Re: Lotto programm
das programm geht jetzt danke hab den klienen fehler behoben aber jetzt kommt keine showmessage
|
Re: Lotto programm
was progman
kannst du das bitte erklären |
Re: Lotto programm
Delphi-Quellcode:
Warum kommt jetzt keine showemssage
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Button1: TButton; Button2: TButton; Button3: TButton; Edit7: TEdit; Edit8: TEdit; Edit9: TEdit; Edit10: TEdit; Edit11: TEdit; Edit12: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var a,b,c,d,e,f :integer; richtig: boolean; falsch: boolean; begin randomize; repeat a:= random (46); b:= random (46); c:= random (46); d:= random (46); e:= random (46); f:= random (46); until (a<>b) and (a<>c) and (a<>d) and (a<>e) and (a<>f) and (b<>a) and (b<>c) and (b<>d) and (b<>e) and (b<>f) and (c<>a) and (c<>b) and (c<>d) and (c<>d) and (c<>f) and (d<>a) and (d<>b) and (d<>c) and (d<>e) and (d<>f) and (e<>a) and (e<>b) and (e<>c) and (e<>d) and (e<>f) and (f<>a) and (f<>b) and (f<>c) and (f<>d) and (f<>e); edit1.Text := inttostr (a+1); edit2.Text := inttostr (b+1); edit3.Text := inttostr (c+1); edit4.Text := inttostr (d+1); edit5.Text := inttostr (e+1); edit6.Text := inttostr (f+1); if StrToInt( edit1.text) = StrToInt( edit7.text) then richtig := true ; if StrToInt( Edit2.text) = StrToInt( Edit8.text) then richtig := true ; if StrToInt( Edit3.text) = StrToInt( Edit9.text) then richtig := true ; if StrToInt( Edit4.text) = StrToInt( Edit10.text) then richtig := true ; if StrToInt( Edit5.text) = StrToInt( Edit11.text) then richtig := true ; if StrToInt( Edit6.text) = StrToInt( Edit12.text) then richtig := true ; if richtig then showmessage ('Sie haben Gewonnen'); if StrToInt( Edit1.text) <> StrToInt( Edit7.text) then falsch := false ; if StrToInt( Edit2.text) <> StrToInt( Edit8.text) then falsch := false ; if StrToInt( Edit3.text) <> StrToInt( Edit9.text) then falsch := false ; if StrToInt( Edit4.text) <> StrToInt( Edit10.text) then falsch := false ; if StrToInt( Edit5.text) <> StrToInt( Edit11.text) then falsch := false ; if StrToInt( Edit6.text) <> StrToInt( Edit12.text) then falsch := false ; if falsch then showmessage ('Sie haben leider verloren'); end; procedure TForm1.Button2Click(Sender: TObject); begin close; end; procedure TForm1.Button3Click(Sender: TObject); begin edit1.clear; edit2.clear; edit3.clear; edit4.clear; edit5.clear; edit6.clear; edit7.clear; edit8.clear; edit9.clear; edit10.clear; edit11.clear; edit12.clear; end; end. |
Re: Lotto programm
Weil die Methode so nicht klappt. Die Variablen falsch und richtig werden jedesmal gesetzt und behalten demzufolge nur ihren letzten Wert.
Besser wäre es:
Delphi-Quellcode:
var richtige: Integer;
richtige:=0; if StrToInt( edit1.text) = StrToInt( edit7.text) then inc(richtige); //immer um 1 erhöhen, wenns stimmt if StrToInt( Edit2.text) = StrToInt( Edit8.text) then inc(richtige) ; if StrToInt( Edit3.text) = StrToInt( Edit9.text) then inc(richtige) ; if StrToInt( Edit4.text) = StrToInt( Edit10.text) then inc(richtige) ; if StrToInt( Edit5.text) = StrToInt( Edit11.text) then inc(richtige) ; if StrToInt( Edit6.text) = StrToInt( Edit12.text) then inc(richtige) ; if richtige = 6 then ShowMessage('Hurra!'); |
Re: Lotto programm
[Pascal Fehler] Unit1.pas(79): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Integer' gefunden
[Pascal Fehler] Unit1.pas(80): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Integer' gefunden [Pascal Fehler] Unit1.pas(81): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Integer' gefunden [Pascal Fehler] Unit1.pas(82): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Integer' gefunden [Pascal Fehler] Unit1.pas(83): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Integer' gefunden [Pascal Fehler] Unit1.pas(84): E2014 Anweisung erforderlich, aber Ausdruck vom Typ 'Integer' gefunden
Delphi-Quellcode:
richtige :=0;
if StrToInt( edit1.text) = StrToInt( edit7.text) then inc (richtige); if StrToInt( Edit2.text) = StrToInt( Edit8.text) then inc (richtige); if StrToInt( Edit3.text) = StrToInt( Edit9.text) then inc (richtige); if StrToInt( Edit4.text) = StrToInt( Edit10.text) then inc (richtige); if StrToInt( Edit5.text) = StrToInt( Edit11.text) then inc (richtige); if StrToInt( Edit6.text) = StrToInt( Edit12.text) then inc (richtige); if richtige = 6 then showmessage ('Sie haben Gewonnen'); if StrToInt( Edit1.text) <> StrToInt( Edit7.text) then (falsche); if StrToInt( Edit2.text) <> StrToInt( Edit8.text) then (falsche); if StrToInt( Edit3.text) <> StrToInt( Edit9.text) then (falsche); if StrToInt( Edit4.text) <> StrToInt( Edit10.text) then (falsche); if StrToInt( Edit5.text) <> StrToInt( Edit11.text) then (falsche); if StrToInt( Edit6.text) <> StrToInt( Edit12.text) then (falsche); if falsche =6 then showmessage ('Sie haben leider verloren'); |
Re: Lotto programm
Kannst du einfach mal deinen code selber nochmal lesen, anstatt ihn blind zum x-ten Male wieder hier reinzuposten, damit wir den Fehler wieder suchen sollen...
Lies mal diese Zeile, und überlege mal kurz, was daran wohl nicht stimmt:
Delphi-Quellcode:
if StrToInt( Edit1.text) <> StrToInt( Edit7.text) then (falsche);
|
Re: Lotto programm
sorry
|
Re: Lotto programm
und "falsche" ebenfalls vorher auf null setzen, damit nicht irgendwas zufälliges drin steht und dann 5362 rauskommt. :lol: ;)
|
Re: Lotto programm
Thx für die guten tipps
|
Re: Lotto programm
Ich hab da auch noch eine Lösung:
Delphi-Quellcode:
[edit] Ich weiß, dass man u.U. mogeln kann, indem man in alle Edits die gleiche Zahl eingibt. Aber mir ging es jetzt darum, eine Möglichkeit mit einer Menge aufzuzeigen. [/edit]
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TZahlen = set of byte; TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private-Deklarationen } Zahlen: TZahlen; procedure Ziehung; public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Ziehung; var Zahl, i: byte; begin Zahlen := []; for i := 1 to 6 do begin repeat Zahl := Random(45) + 1; until not (Zahl in Zahlen); include(Zahlen,Zahl); end; end; procedure TForm1.FormCreate(Sender: TObject); begin Randomize; end; procedure TForm1.Button1Click(Sender: TObject); var Treffer: byte; begin Ziehung; Treffer := 0; if (StrToIntDef(Edit1.Text,0) in Zahlen) then inc(Treffer); if (StrToIntDef(Edit2.Text,0) in Zahlen) then inc(Treffer); if (StrToIntDef(Edit3.Text,0) in Zahlen) then inc(Treffer); if (StrToIntDef(Edit4.Text,0) in Zahlen) then inc(Treffer); if (StrToIntDef(Edit5.Text,0) in Zahlen) then inc(Treffer); if (StrToIntDef(Edit6.Text,0) in Zahlen) then inc(Treffer); ShowMessage(Format('%d Richtige',[Treffer])); end; end. |
Re: Lotto programm
ist e so ähnlcih
|
Re: Lotto programm
Aber einfacher ;)
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:22 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz