Autor Beitrag
roadster
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17


D 7 PE
BeitragVerfasst: Do 09.02.06 22:23 
Hallo,

mein Sohn und ich versuchen uns gerade an unserem ersten Programm,
Eigenlich ganz einfach, aber irgendwie haben wir einen Denkfehler eingebracht.

Das Programm generiert eine Zahl zwischen 10 und 25, setzt die Zahl zum Quadrat und vergleicht
diese dann mit der Eingabe. Lt. der If Bedingung ist das Ergebnis allerdings immer falsch??! :?:

Vielen Dank für Eure Hilfe

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.Button1Click(Sender: TObject);
begin
      randomize;
      try
        Frage := random(15) +10 ;
        label1.Caption := inttostr(Frage);
        edit1.SetFocus;
        eingabe:= strtoint(edit1.Text);
        label3.Caption := inttostr(Frage*Frage);
        label4.Caption := inttostr(eingabe);
        if eingabe = frage*frage then
            label2.Caption := 'Glück gehabt'
        else
            label2.Caption := 'Hmmm';
      except
      end;
end;

end.


Moderiert von user profile iconGausi: Beitragsformatierung überarbeitet.
_Digger
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 46

Win XP
Delphi5 Prof
BeitragVerfasst: Do 09.02.06 22:33 
ich kann eigentlich keinen Fehler erkennen.
eine Frage: was hat das programm mit Primzahlen zu tun?
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Do 09.02.06 22:38 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
if eingabe = frage*frage then  // <= 
            label2.Caption := 'Glück gehabt'
        else
            label2.Caption := 'Hmmm';

Ich weiß nicht welche werte ihr eingeben wollt... aber beispielweiße, eure zufallszahl is 25... dann müsste im editfeld 625 stehen... dann klappts auch...
sollen aber nur zahlen von 10-25 erraten werden, würde ich in der IF anweisung, frage nicht quadrieren :roll:

... und auch meine Frage... was hat das mit Primzahlen zu tun? :gruebel:
roadster Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17


D 7 PE
BeitragVerfasst: Do 09.02.06 22:40 
Hallo,

zum Üben von Primfaktorzerlegung müssen die Kids alle Quadratzahlen von 11 bis 25 Auswendiglernen.
Das Programm sollte da ein wenig helfen - aber leider ist irgendwie der Wurm drin. Der Vergleich der Eingabe passiert immer mit der nächsten Zufallszahl; keinen Schimmer warum.
Bruder Lal
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 31

WIN XP
Delphi 2005
BeitragVerfasst: Do 09.02.06 22:44 
Bei dem Button, auf den man nach der Eingabe drückt, wird ja schon eine neue Zufallszahl generiert. Das lässt sich beheben indem du den code leicht umstellst:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.Button1Click(Sender: TObject);
begin
      randomize;
      try
        eingabe:= strtoint(edit1.Text);
        label3.Caption := inttostr(Frage*Frage);
        label4.Caption := inttostr(eingabe);
        if eingabe = frage*frage then
            label2.Caption := 'Glück gehabt'
        else
            label2.Caption := 'Hmmm';
        Frage := random(15) +10 ;
        label1.Caption := inttostr(Frage);
        edit1.SetFocus;
      except
      end;
end;

end.

Wozu eigentlich try.. except?
edit: Und randomize sollte möglichst nur einmal aufgerufen werden.. z.b. in FormCreate
roadster Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17


D 7 PE
BeitragVerfasst: Do 09.02.06 22:53 
Super, :D

hat mich 2h meines Lebens gekostet.


try.. except habe ich verwendet um Fehlermeldungen bei zb. einer Eingabe von Buchstaben abzufangen.

FormCreate sagt mir momentan noch nix. :?:


Vielen Dank
Bruder Lal
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 31

WIN XP
Delphi 2005
BeitragVerfasst: Do 09.02.06 23:00 
Also ich würde ja die Fehlerursache zerstören.. z.B. so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function MakeInteger(s: string): string;
var i: Integer;
begin
  result:='';
  for i:= 1 to length(s) do
    if s[i] in['0'..'9'then result:=result+s[i];
end;

aber das ist deine Entscheidung. Und das Ereignis OnCreate wird ausgelöst wenn das Programm startet. Such mal im Objektinspektor bei Form1 unter Ereignisse..
roadster Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17


D 7 PE
BeitragVerfasst: Do 09.02.06 23:27 
Die Oberfläche von Delphi ist für mich noch ne echte Herausforderung.

Danke für die Hilfe
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Do 09.02.06 23:50 
Mach Dir keine Sorgen um die Delphi Oberfläche, es wird Komplizierter !!!
Quake User
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 159



BeitragVerfasst: Fr 10.02.06 13:31 
mein Sohn und ich versuchen uns gerade an unserem ersten Programm,
Eigenlich ganz einfach, aber irgendwie haben wir einen Denkfehler eingebracht.

Das Programm generiert eine Zahl zwischen 10 und 25, setzt die Zahl zum Quadrat und vergleicht
diese dann mit der Eingabe. Lt. der If Bedingung ist das Ergebnis allerdings immer falsch??!

Dein erstes Programm sollte "Hello World" sein. (such mal ein Tutorial) So hast Du alle Funktionen der Delphi IDE schon einmal verwendet.

Du brauchst:
- Haltepunkte
- Überwachte Ausdrücke
- starte das programm "Zeilenweise" (F7, SHIFT+F7)


Zuletzt bearbeitet von Quake User am Fr 10.02.06 13:59, insgesamt 1-mal bearbeitet
Quake User
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 159



BeitragVerfasst: Fr 10.02.06 13:55 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.Button1Click(Sender: TObject);
begin
      randomize;
      try
        Frage := random(15) +10 ;
        label1.Caption := inttostr(Frage);
        edit1.SetFocus;
        eingabe:= strtoint(edit1.Text);
        label3.Caption := inttostr(Frage*Frage);
        label4.Caption := inttostr(eingabe);
        if eingabe = frage*frage then
            label2.Caption := 'Glück gehabt'
        else
            label2.Caption := 'Hmmm';
      except
      end;
end;

end.


mal noch einige Tips zum Design:
- verzichte am Anfang auf Fehlerbehandlung. Du solltest dazu später ein gutes Buch lesen.
Fehlerbehandlungen würde ich einbauen, wenn dein Programm getestet ist und unter Normalbedingungen sauber läuft

- Für jede Aktion solltest Du einen Event verwenden. (am Einfachsten ist dies am Anfang mit ButtonClick) später kannst Du dann auch Events wie OnShow, OnCreate ... verwenden.

- "edit1.SetFocus;" Nicht Du steuerst das Programm, sondern der Anwender am Monitor. Wenn er den Fokus auf das Edit Feld setzen möchte, wird er es tun.

- Verwende "sprechende" Ausgaben "Hmmm" sagt dem Nutzer eigentlich was?

- Dokumentiere deine Quellen

meine abgeänderte Variante. (ungetestet)
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:
// Ausgangszustand erzeugen
procedure TForm1.Button1Click(Sender: TObject);
begin
  Frage := random(15) +10 ;
  label1.Caption := inttostr(Frage);
  label3.Caption := inttostr(Frage*Frage);
end;

// Test
procedure TForm1.Button2Click(Sender: TObject);
begin
  eingabe:= strtoint(edit1.Text);
  label4.Caption := inttostr(eingabe);
        if eingabe = (frage*frage) then
            label2.Caption := 'Du hast '+inttostr(eingabe)+ ' eingegeben. Glück gehabt'
        else
            label2.Caption := 'Du hast '+inttostr(eingabe)+ ' eingegeben. Hmmm';
end;

// Initialisierung
procedure TForm1.Button3Click(Sender: TObject);
begin
  randomize;
end;


Ich bin mir über den Sinn des Programms noch nicht ganz im klaren.