Autor Beitrag
navarro2010
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25
Erhaltene Danke: 1



BeitragVerfasst: So 16.01.11 19:45 
Ich habe eine Aufgabe in Delphi zu lösen, bei der man eine Zufallsvariable zwischen 0 und 100 erstellen soll.
Je nach Eingabe soll gezeigt werden, ob die geratene Zahl zu groß, zu klein oder richtig ist.
Das ganze habe ich mit einer if-then else verzweigung gemacht, es kommt trotzdem eine fehlermeldung.
Kann mir jemand einen Tipp geben, oder hat jemand sogar einen Quelltext?
Vielen Dank


Moderiert von user profile iconMartok: Topic aus Sonstiges (Delphi) verschoben am So 16.01.2011 um 19:20
HenryHux
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 542
Erhaltene Danke: 33

Windows 7 Premium
Delphi XE, Eclipse
BeitragVerfasst: So 16.01.11 19:50 
Du kannst Zufallszahlen mit random erstellen.
Damit das nicht jedesmal die gleiche ist, benutzt du am besten randomize.
Sähe so aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
 
randomize;
a := random(100);
// hier die IF's


Was klappt denn nicht bzw was ist die Fehlermeldung bzw wie sieht dein Source aus?

Lg
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: So 16.01.11 19:54 
Ohne Fehlermeldung und ohne funktionierende Glaskugel wird dir niemand helfen können...

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
navarro2010 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25
Erhaltene Danke: 1



BeitragVerfasst: So 16.01.11 20:19 
Mein Quelltext:

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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Pa1: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  y:integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  y:=random(100);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if(random>y) then
  Pa1.Caption:='Zahl zu klein';

  else if (random<y) then
  Pa1.Caption:='Zahl zu groß';
end
else
begin
  Pa1.Caption:='Gewonnen'
end;


Moderiert von user profile iconMartok: Delphi-Tags gesetzt
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: So 16.01.11 20:22 
Hallo!

Bitte verwende für Quellcode die entsprechenden [delphi]-Tags, dann wird er "schöner" dargestellt als im Fließtext. Beispiel:
ausblenden Quelltext
1:
<span class="inlineSyntax"><span class="codecomment">{PROTECTTAG30b6d3db2097bf140daf7e6ebfebbabb}</span></span>					

Wird:
ausblenden Delphi-Quelltext
1:
var Test: integer;					


Ich hab das mal für dich erledigt, fürs nächste mal dann bitte beachten.

Viele Grüße,
Martok

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: So 16.01.11 20:41 
1) Bitte eine ganz genaue Fehlerbeschreibung abgeben.

2) Mit dem Aufruf von 'random' erzeugst du an der jeweiligen Stelle eine neue Zufallszahl, was du wahrscheinlich nicht möchtest. Wie gesagt, bitte sag uns doch, was für ein Problem du hast...

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
ALF
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1085
Erhaltene Danke: 53

WinXP, Win7, Win10
Delphi 7 Enterprise, XE
BeitragVerfasst: So 16.01.11 20:48 
schau mal genau hin

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.Button1Click(Sender: TObject);
begin
  if(random>y) then
  Pa1.Caption:='Zahl zu klein';

  else if (random<y) then
  Pa1.Caption:='Zahl zu groß';
end
else
begin
  Pa1.Caption:='Gewonnen'
end;
und für was soll die 'edit' Komponente sein, wenn Du sie nicht als Eingabe der Zahl, die du sicherlich vergleichen willst, auch benutzt!
Vermute ich mal oder?

Gruss Alf

_________________
Wenn jeder alles kann oder wüsste und keiner hätt' ne Frage mehr, omg, währe dieses Forum leer!
navarro2010 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25
Erhaltene Danke: 1



BeitragVerfasst: So 16.01.11 20:50 
Das Problem ist, dass wenn ich das Programm ausführe, gibt es nivht den Fall, dass ich richtig rate .
Wenn ich 0 eintippe kommt Zahl zu klein und wenn ich 1 eingebe kommt die Zahl ist zu groß.
ALF
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1085
Erhaltene Danke: 53

WinXP, Win7, Win10
Delphi 7 Enterprise, XE
BeitragVerfasst: So 16.01.11 20:59 
ok Du hast wahrscheinlich nicht alles gelesen oder!?
Und eigentlich müsste das doch gar nicht starten
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if(random>y) then  
   Pa1.Caption:='Zahl zu klein';
else if (random<y) then
 .....


Hier ne kleine Hilfe
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure TForm1.Button1Click(Sender: TObject);
begin
  y:= random(100);
  if y > strtoint(edit1.text) then
     Pa1.Caption:= 'Zahl zu klein'
  else
    .....
    .....

end;
und der Rest ergibt sich beim genauen hinschauen :wink:

Gruss ALf

_________________
Wenn jeder alles kann oder wüsste und keiner hätt' ne Frage mehr, omg, währe dieses Forum leer!
navarro2010 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25
Erhaltene Danke: 1



BeitragVerfasst: Mo 17.01.11 21:08 
Kann mir noch jemand einen Tipp geben?
Es kommt nicht, dass man gewonnen hat

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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Pa1: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  y:integer;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  randomize;
  y:=random(100);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  y:= random(100);
  if y > strtoint(Edit1.Text) then
     Pa1.Caption:= 'Zahl zu klein'
  else if y < strtoint(Edit1.Text) then
     Pa1.Caption:= 'Zahl zu groß'
  else
     Pa1.Caption:= 'Gewonnen'




end;
end.
HenryHux
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 542
Erhaltene Danke: 33

Windows 7 Premium
Delphi XE, Eclipse
BeitragVerfasst: Mo 17.01.11 21:10 
Haha, überleg dir mal kurz weswegen dir hier :
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
  y:= random(100);
  if y > strtoint(Edit1.Text) then
     Pa1.Caption:= 'Zahl zu klein'
  else if y < strtoint(Edit1.Text) then
     Pa1.Caption:= 'Zahl zu groß'
  else
     Pa1.Caption:= 'Gewonnen'


Nicht immer Gewonnen angezeigt wird :lol:

EDIT:

Ich glaube ich verstehe wo wirklich dein Problem ist. Du versucht wahrscheinlich, anhand der Tipps die richtige Zahl zu suchen.
Dann musst du aber das random rausnehmen, denn das generiert jedes mal eine neue Zahl =)
navarro2010 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 25
Erhaltene Danke: 1



BeitragVerfasst: Mo 17.01.11 21:15 
Ich weiß nicht warum nicht gewonnen angezeigt wird, vielleicht kannst du mir weiterhelfen?
HenryHux
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 542
Erhaltene Danke: 33

Windows 7 Premium
Delphi XE, Eclipse
BeitragVerfasst: Mo 17.01.11 21:27 
Ja, du willst bei jedem Button-Click die Zahl raussuchen.
Aber bei jedem Button-Click generiert sich eine neue Zahl, deswegen steht jedesmal die Chance 1 zu 100, dass gewonnen angezeigt wird.
Also versuchst du es entweder den halben Abend, oder du legst das generieren der Zahl auf einen anderen Button =)