Autor Beitrag
Florian.K
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

Win Xp Prof. & SP 2
Delphi 10 Lite
BeitragVerfasst: So 05.03.06 14:23 
edit: Hi Community =)
Ist es möglich ein Programm zu schreiben womit ich per Eingabe in einer Inputbox eine Zahl eingebe und ich diese solange eingeben muss bis die randomierte Zahl erraten wurde? Ich würde Anfangen und den randomierten Zahlenbereich auf 1-40 zu setzen oder um kleiner anzufangen 1-10. Nur weiter? danke im Vorraus @Florian
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: So 05.03.06 14:43 
Das riecht ja nach Hausaufgaben ;) dieses klassische Wir raten eine Zahl Spiel.

Überlege doch mal in Ruhe wie der Ablauf sein müsste...
1.Benutzer überlegt Zufallszahl
2.Programm sucht sich eine Zufallszahl aus
3.Benutzer sagt ob es die war, ob sie kleiner oder größer ist
4.Wenn es die war Ende
Wenn sie kleiner Ist probier eine aus einem kleineren Zahlenbereich(0 - AlteZahl-1) und zu 3.
Wenn sie größer ist aus dem größeren Zahlenbereich(Altezahl+1 - obere Grenze) und zu 3.

Erweiterungsvorschlag: baue sowas für Buchstaben :-)
Florian.K Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

Win Xp Prof. & SP 2
Delphi 10 Lite
BeitragVerfasst: So 05.03.06 15:41 
nix Hausaufgaben aber thx
Florian.K Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

Win Xp Prof. & SP 2
Delphi 10 Lite
BeitragVerfasst: So 05.03.06 16:48 
Kriegs nicht hin, guckt euch mein qT gewusel mal an.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
var Zahl,Eingabe,Versuche,Zufallszahl:Integer;
begin
Versuche:=0;
Zahl:=InputBox('Zahl zwischen 1 und 20', 'Deine Zahl',
REPEAT
Zufallszahl:=Random(20)+1;
IntToStr(Zufallszahl);
Zahl :=Eingabe;
Versuche:=Versuche+1;
if Zahl<Zufall then begin
Showmessage('Zahl ist zu klein');
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: So 05.03.06 17:01 
Ich frag zur Sicherheit nochmal nach, willst du
a) Du dir eine Zahl ausdenken, dein Programm errät sie indem du > oder < sagst oder
b) Er sich eine Zahl ausdenkt und du musst sie erraten und er sagt > oder < ?

Ich dachte du meinst a) aber dein Code sieht nach b) aus
M.p.3
Hält's aus hier
Beiträge: 10

WinXP Home SP 2
Delphi 7 PE, Delphi 2005 PE
BeitragVerfasst: So 05.03.06 17:05 
er meint schon b.. weil wir hattn au b.. genau 's gleiche.. un 's warn hausaufgabn +flame+ xD
nja was brauchstn jetz genau.. wo issn 's problem (hab de source nich großartig angeguggt.. aber ich hat 'n bissl mehr xD)

mfg Mp3
jonas_hirner
Hält's aus hier
Beiträge: 10

WinXP
Delphi 2006 Pro
BeitragVerfasst: So 05.03.06 17:24 
Hoi,
also bei deiner source steig ich nicht so wirklich durch.
Hab da mal als Inspiration schnell was geschrieben:

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:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
unit Unit1;

interface

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

type
  TFormMain = class(TForm)
    EditInputBox: TEdit;
    LabelOutput: TLabel;
    ButtonRaten: TButton;
    ButtonClose: TButton;
    ButtonNeu: TButton;
    LabelVersuche: TLabel;
    procedure ButtonRatenClick(Sender: TObject);
    procedure ButtonNeuClick(Sender: TObject);
    procedure ButtonCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  FormMain: TFormMain;
  GeheimZahl, Versuche: Integer;


implementation

{$R *.dfm}

procedure NeuesSpiel;
begin
  FormMain.EditInputBox.Clear;
  GeheimZahl:= Random(20) + 1;
  Versuche:= 0;

  FormMain.LabelOutput.Caption:= 'Zahl raten!';
  FormMain.LabelVersuche.Caption:= 'Versuche: 0';
end;

procedure TFormMain.ButtonCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TFormMain.ButtonNeuClick(Sender: TObject);
begin
  NeuesSpiel;
end;

procedure TFormMain.ButtonRatenClick(Sender: TObject);
begin
  if StrToInt(EditInputBox.Text) = GeheimZahl then
    begin
      MessageBox(Self.Handle, 'Richtig geraten!', 'Glückwunsch', 0);
      NeuesSpiel;
    end
  else if StrToInt(EditInputBox.Text) < GeheimZahl then
    begin
      Inc(Versuche);
      LabelOutput.Caption:= 'Die gesuchte Zahl ist grösser';
      LabelVersuche.Caption:= 'Versuche: ' + IntToStr(Versuche);
    end
  else
    begin
      Inc(Versuche);
      LabelOutput.Caption:= 'Die gesuchte Zahl ist kleiner';
      LabelVersuche.Caption:= 'Versuche: ' + IntToStr(Versuche);
    end;
end;

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

end.


Es dürfen natürlich nur Zahlen in das Edit eingegeben werden, ne sicherung is jetzt nicht drin. Ist ja auch nur ein Beispiel ;)

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt
Müscha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 118



BeitragVerfasst: So 05.03.06 17:32 
ähmmm bei deinem Quelltext fällt mir aus, dass du nach repeat begin vergessen hast!!!!!!!
Florian.K Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

Win Xp Prof. & SP 2
Delphi 10 Lite
BeitragVerfasst: So 05.03.06 21:48 
Aber wo ist mein Fehler, Also ich krieg 1. die Repeat nicht eingebaut und 2. er Compiled es nicht meckert beim end.
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 TForm3.Button3Click(Sender: TObject);
var Zahl:Integer;
ZufallZahl,Versuch,i:Integer;
begin
Zufallzahl:=0;
Versuch:=0;
ZufallZahl:=Random(20)+1;
begin
InputBox('Rate mal', 'Zahl eintippen', IntToStr (Zahl));
  begin
  if Zahl < ZufallZahl then begin
  LblErgebnis.Caption := 'Deine Zahl ist zu klein!';
  Versuch := Versuch + 1;
  end;
  If Zahl> ZufallZahl Then begin
  Begin
  LblErgebnis.Caption := 'Deine Zahl ist zu groß!';
  Versuch := Versuch + 1;
  end;
  If Zahl = ZufallZahl Then begin
Begin
LblErgebnis.Caption := 'RICHTIG!!!';
END;
End;

end;

end.


Zuletzt bearbeitet von Florian.K am So 05.03.06 21:57, insgesamt 1-mal bearbeitet
Ironwulf
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 733
Erhaltene Danke: 2



BeitragVerfasst: So 05.03.06 21:52 
der fehler mit dem repeat kommt weil zu jedem repeat ein until gehört und der fehler mit dem end kommt weil in zeile 10 ein begin steht und zu jedem begin auch ein end; gehört
jonas_hirner
Hält's aus hier
Beiträge: 10

WinXP
Delphi 2006 Pro
BeitragVerfasst: So 05.03.06 21:53 
also in dem angegebenen codeschnipsel fehlt die Abbruchkondition zum repeat.
Beispiel:

Repeat
...
Until Zahl = Zufall
Florian.K Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

Win Xp Prof. & SP 2
Delphi 10 Lite
BeitragVerfasst: So 05.03.06 21:59 
sagmal ich kriegs nicht gebacken ich bin doch nah dran
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:
procedure TForm3.Button3Click(Sender: TObject);
var Zahl:Integer;
ZufallZahl,Versuch,i:Integer;
begin
Zufallzahl:=0;
Versuch:=0;
ZufallZahl:=Random(20)+1;
begin
InputBox('Rate mal', 'Zahl eintippen', IntToStr (Zahl));
end;
  begin
  REPEAT
  if Zahl < ZufallZahl then begin
  LblErgebnis.Caption := 'Deine Zahl ist zu klein!';
  Versuch := Versuch + 1;
  end;
  If Zahl> ZufallZahl Then begin
  Begin
  LblErgebnis.Caption := 'Deine Zahl ist zu groß!';
  Versuch := Versuch + 1;
  end;
  If Zahl = ZufallZahl Then begin
Begin
LblErgebnis.Caption := 'RICHTIG!!!';
UNTIL Zahl= ZufallZahl ;
END;
End;

end;

end.
Ironwulf
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 733
Erhaltene Danke: 2



BeitragVerfasst: So 05.03.06 22:08 
was mir auffält ist das du
zufallszahl und versuch in der onclick prozedur deklarierst, dort gehörts schonmal gar nicht hin, denn jedes mal wenn man auf den button drückt kommt eine neue zufallszahl und versuche wird wieder auf 0 gesetzt
deine repeat schleife ist vollkommen überflüssig, da die inputbox davor kommt, entweder du machst die inputbox mit in die schleife oder du lässt sie weg, wenn du dich für das erste entscheidest kannst du auch die variablen deklaration so lassen

und sonst fällt noch auf:
ausblenden Delphi-Quelltext
1:
2:
If Zahl> ZufallZahl Then begin
  Begin

das ist vollkommen überflüssig da 2 mal begin zu nehmen

ausblenden Delphi-Quelltext
1:
2:
3:
4:
If Zahl = ZufallZahl Then begin  
  end;  
Begin  
LblErgebnis.Caption := 'RICHTIG!!!';

das macht auch wenig sinn, da die if auswahl beendet wird ohne das was passiert und das programm einfach weiter läuft, so wirst du immer in deinem label "RICHTIG!!!" stehn haben
Ironwulf
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 733
Erhaltene Danke: 2



BeitragVerfasst: So 05.03.06 22:17 
aus langerweile hab ichs jetz mal 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:
procedure TForm1.Button1Click(Sender: TObject);
var Zahl, Versuch, ZufallsZahl: Byte;
begin
  Versuch:= 0;
  Zahl:= 5;
  ZufallsZahl:= Random(10)+1;
  Repeat
    zahl:= StrToInt(InputBox('Rate mal','bla' ,IntToStr(Zahl)));
    If Zahl < ZufallsZahl Then
    begin
      Label1.Caption:= 'zu Klein';
      inc(versuch);
    end;

    If  Zahl > Zufallszahl Then
    begin
      Label1.Caption:= 'zu Groß';
      inc(Versuch);
    end;
  Until Zahl = zufallszahl;

  Label1.Caption:= 'Richtig';
  Label2.Caption:= StrToInt(Versuch);
end;
Florian.K Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

Win Xp Prof. & SP 2
Delphi 10 Lite
BeitragVerfasst: So 05.03.06 22:25 
so läufts: nur er zeigt mir immer an RICHTIG!! das ding haut mich um
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
var Zahl:Integer;
ZufallZahl,Versuch,i:Integer;
begin
Zufallzahl:=0;
Versuch:=0;
ZufallZahl:=Random(20)+1;
begin
REPEAT
InputBox('Rate mal', 'Zahl eintippen', IntToStr (Zahl));
  if Zahl < ZufallZahl then begin
  LblErgebnis.Caption := 'Deine Zahl ist zu klein!';
  Versuch := Versuch + 1;
  end;
  If Zahl> ZufallZahl Then begin
  LblErgebnis.Caption := 'Deine Zahl ist zu groß!';
  Versuch := Versuch + 1;
  end;
  If Zahl = ZufallZahl Then begin
  end;
LblErgebnis.Caption := 'RICHTIG!!!';
UNTIL (Zahl=ZufallZahl) ;
Ironwulf
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 733
Erhaltene Danke: 2



BeitragVerfasst: So 05.03.06 22:34 
verdreh mal zeile 20 und 19
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: So 05.03.06 22:38 
user profile iconFlorian.K hat folgendes geschrieben:
so läufts: nur er zeigt mir immer an RICHTIG!! das ding haut mich um
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
var Zahl:Integer;
ZufallZahl,Versuch,i:Integer;
begin
Zufallzahl:=0;
Versuch:=0;
ZufallZahl:=Random(20)+1;
begin
REPEAT
  InputBox('Rate mal', 'Zahl eintippen', IntToStr (Zahl));
  if Zahl < ZufallZahl then begin
    LblErgebnis.Caption := 'Deine Zahl ist zu klein!';
    Versuch := Versuch + 1;
  end;
  If Zahl> ZufallZahl Then begin
    LblErgebnis.Caption := 'Deine Zahl ist zu groß!';
    Versuch := Versuch + 1;
  end;
  If Zahl = ZufallZahl Then begin
    LblErgebnis.Caption := 'RICHTIG!!!';
  end;
UNTIL (Zahl=ZufallZahl) ;


Guter tipp ist auch, den Code richtig zu Formatieren, denn dann hättest du gesehen, dass deine "richtig" Nachricht ausserhalb des Begin-end; blocks steht.

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
Florian.K Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

Win Xp Prof. & SP 2
Delphi 10 Lite
BeitragVerfasst: So 05.03.06 22:55 
okok =), nur krieg ich jetzt garnichts mehr angezeigt als Ergebnis!
Edit: Das proggi haut mich noch um ^^
Edit: also jetzt meldet er mir Zahl ist zu klein ^^


Zuletzt bearbeitet von Florian.K am So 05.03.06 23:00, insgesamt 1-mal bearbeitet
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: So 05.03.06 22:58 
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:
var Zahl:Integer;
ZufallZahl,Versuch,i:Integer;
begin
Zufallzahl:=0;
Versuch:=0;
ZufallZahl:=Random(20)+1;
begin
REPEAT
  InputBox('Rate mal', 'Zahl eintippen', IntToStr (Zahl));
  if Zahl < ZufallZahl then begin
    LblErgebnis.Caption := 'Deine Zahl ist zu klein!';
    Versuch := Versuch + 1;
  end;
  If Zahl> ZufallZahl Then begin
    LblErgebnis.Caption := 'Deine Zahl ist zu groß!';
    Versuch := Versuch + 1;
  end;
  If Zahl = ZufallZahl Then begin
    LblErgebnis.Caption := 'RICHTIG!!!';
  end;
Application.ProcessMessages; //<-- Ohne das wird ERST die Schleife abgearbeitet, und DANN das Label verändert.
UNTIL (Zahl=ZufallZahl) ;

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
Florian.K Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130

Win Xp Prof. & SP 2
Delphi 10 Lite
BeitragVerfasst: So 05.03.06 23:11 
Läuft ja das Programm : D , halt er zeigt mir nur an Die Zahl ist zu klein.
Und wie krieg ich es hin das wenn ich in der inputbox auf close klicke er die box schließt
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
begin
REPEAT
InputBox('Rate mal', 'Zahl eintippen', IntToStr (Zahl));
  if Zahl < ZufallZahl then begin
  lblErgebnis.Caption := 'Deine Zahl ist zu klein!';
  Versuch := Versuch + 1;
  pnl1.caption:=IntToStr(Versuch);
  end;
  If Zahl> ZufallZahl Then begin
  lblErgebnis.Caption := 'Deine Zahl ist zu groß!';
  Versuch := Versuch + 1;
  pnl1.caption:=IntToStr(Versuch);
  end;
  If Zahl = ZufallZahl Then begin
  LblErgebnis.Caption := 'RICHTIG!!!';
  end;
  Application.ProcessMessages;
  UNTIL (Zahl=ZufallZahl) ;
END;