Autor Beitrag
somebody
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 167
Erhaltene Danke: 2



BeitragVerfasst: Di 08.05.07 11:22 
Hi, ich habe ein Problem bei Delphi, wie eigentlich jeder,

Wie gewöhnt man Delphi ab, das es immer die else abfrage macht?

mfg:somebody


Moderiert von user profile iconGausi: Topic aus Sonstiges (Delphi) verschoben am Di 08.05.2007 um 13:29
FiceGoesDelphi
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 485

Win XP Prof
Delphi 2006
BeitragVerfasst: Di 08.05.07 11:26 
Liegt vllt daran, dass ich nicht gerade der Delphi-Experte bin!
Aber ich verstehe dein Problem nicht ganz!
Wenn du eine Bedingung hast die erfüllt sein muss, damit etwas passiert, also du "IF" benutzt, dann MUSS doch eigentlich KEIN else kommen. Bei mir funktioniert es einwandfrei. Wenn Delphi bei dir automatisch "Else" schreibt, kannst du es doch einfach wieder löschen oder? :?!?: Vllt konnte ich dir ja helfen, wenn nicht bin ich mal auf die "Lösung" gespannt!

LG,
FGD


EDIT:
Wenn du die Code-Vervollständigung meinst:
Normalerweise bei "ife" kommt dein If + Else .. wenn du nur IF eingibst, gibt es bei mir keine Vervollständigung!

_________________
<(^^<) <(^^)> (>^^)>
===================


Zuletzt bearbeitet von FiceGoesDelphi am Di 08.05.07 11:30, insgesamt 1-mal bearbeitet
somebody Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 167
Erhaltene Danke: 2



BeitragVerfasst: Di 08.05.07 11:29 
Also, bei mir ist das
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
if das und das
then
begin
das und das
end
else 
begin
das und das
end
end;


so, und jetzt macht der immer nur else, wie kann man das delphi abgewöhnen?
ZeitGeist87
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1593
Erhaltene Danke: 20

Win95-Win10
Delphi 10 Seattle, Rad Studio 2007, Delphi 7 Prof., C++, WSH, Turbo Pascal, PHP, Delphi X2
BeitragVerfasst: Di 08.05.07 11:32 
Super Alternative, allerdings nicht immer anwendbar: Suche in der Entwickler-Ecke CASE

_________________
Wer Provokationen, Ironie, Sarkasmus oder Zynismus herauslesen kann soll sie ignorieren um den Inhalt meiner Beiträge ungetrübt erfassen zu können.
ZeitGeist87
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1593
Erhaltene Danke: 20

Win95-Win10
Delphi 10 Seattle, Rad Studio 2007, Delphi 7 Prof., C++, WSH, Turbo Pascal, PHP, Delphi X2
BeitragVerfasst: Di 08.05.07 11:33 
user profile iconsomebody hat folgendes geschrieben:
Also, bei mir ist das
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
if das und das
then
begin
das und das
end
else 
begin
das und das
end
end;


so, und jetzt macht der immer nur else, wie kann man das delphi abgewöhnen?

wie jezt? delphi macht die else selbst?

_________________
Wer Provokationen, Ironie, Sarkasmus oder Zynismus herauslesen kann soll sie ignorieren um den Inhalt meiner Beiträge ungetrübt erfassen zu können.
FiceGoesDelphi
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 485

Win XP Prof
Delphi 2006
BeitragVerfasst: Di 08.05.07 11:35 
Ich verstehs auch noch nicht ganz!
Sag doch mal WAS du eingibst und WAS dann Delphi (automatisch :?!?: ) schreibt!
Und WAS du davon aber nicht haben willst! Also was Delphi NICHT automatisch schreiben soll.
Also was ich dir schonmal sagen kann:

Wenn du Else eingibst ... wird Delphi auch Else ausgebene! :roll: ;)

EDIT:
Ich hab ne Idee :D
Meinst du vllt, dass dein Programm immer nur den Else-Block ausführt????
Dann solltest du deine "If-Bedingung" mal überprüfen.
Und noch ein Tip, der auch mir sehr geholfen hat!

>>> DEBUGGING HILFT <<<

_________________
<(^^<) <(^^)> (>^^)>
===================
somebody Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 167
Erhaltene Danke: 2



BeitragVerfasst: Di 08.05.07 15:28 
Also, ich meine das Delphi immer nur die else Bedingung ausführt, auch wenn die IF Bedingung erfüllt ist.

Wie kann man das beheben?

Aso, die If Bedingung ist richtig.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 08.05.07 15:30 
Zeig mal den Quelltext-Auszug, bei dem es nicht geht (inklusive in welchem Event-Handler es ausgeführt wird) ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
somebody Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 167
Erhaltene Danke: 2



BeitragVerfasst: Di 08.05.07 15:34 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
if
              (aufgabenart = 1) and
              (edit3.text = inttostr(zahl1-zahl2))
          then
              begin
                   panel5.caption := 'Richtig';
                   edit3.color := cllime;
              end
     else
          begin
               panel5.caption := 'Falsch';
               edit3.color := clred;
          end;


aufgabenart, zahl1 und zahl2 habe ich bei var als integer angegeben.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 08.05.07 15:49 
Wenn der else-Zweig ausgeführt wird, bedeutet das, dass die Bedingung "Falsch" ergibt. Also ist entweder Aufgabenart ungleich eins, oder zahl1-zahl2 stimmt nicht mit Edit1.Text überein. Wenn zahl1 und zahl2 richtig in der Prozedur belegt sind (kommt evtl. eine Compilermeldung: Zahl1 könnte undefiniert sein?), dann könnte das an Leerzeichen o.Ä. liegen. Probier die Überprüfung mal andersrum, d.h. StrToInt(Edit1.Text) = zahl1-zahl2

_________________
We are, we were and will not be.
Karlson
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Di 08.05.07 15:57 
Man kann sich aber auch echt bescheuert ausdrücken...sorry :roll:


Der Wahrzweig wird genau dann ausgeführt wenn beide Bedingungen wahr sind. Ist eine oder gar beide unwahr wird der Falschzweig ausgeführt.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
 if (1=1) and (2=2) then
  begin
   showmessage('bedingung wahr');
  end
  else
  begin
   showmessage('bedingung falsch');
  end;


In diesem Beispielcode wird der Wahrzweig ausgeführt, es wird also lediglich Bedingung Wahr angezeigt.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
 if (1=2) and (2=2) then
  begin
   showmessage('bedingung wahr');
  end
  else
  begin
   showmessage('bedingung falsch');
  end;


2=2 ist zwar richtig, aber 1 ist nicht gleich 2, daher wird nur der Else Zweig ausgeführt. Es wird also die    showmessage('bedingung falsch');-Zeile ausgeführt.

Wenn bei dir nur der Else-Zweig ausgeführt wird, dann liegt das folglich daran, dass entweder (aufgabenart <> 1) ist oder das (edit3.text <> inttostr(zahl1-zahl2)) ist.

Wie immer macht Delphi nichts anderes als du ihm sagst.

//Edit: Habs Gausis Posting nicht gelesen.
somebody Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 167
Erhaltene Danke: 2



BeitragVerfasst: Di 08.05.07 15:58 
Ne, geht nicht. Dann führt der wieder nur die else anweisung durch.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 08.05.07 16:01 
Dann zeig mal bitte die gesamte Prozedur. Inklusive Kopf und dem Teil, wo die einzelnen Werte für die Variablen gesetzt werden.

_________________
We are, we were and will not be.
somebody Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 167
Erhaltene Danke: 2



BeitragVerfasst: Di 08.05.07 16:05 
Es trifft aber immer beides zu.

Naja, hier der Delphi Text. Aso, es soll ein Rechenprogramm sein.

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:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel2: TPanel;
    Panel4: TPanel;
    Edit1: TEdit;
    Panel5: TPanel;
    Button2: TButton;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  zahl2 : integer;
  zahl1 : integer;
  aufgabenart : integer;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
     zahl1 := random(10);
     zahl2 := random(10);
     edit1.text := inttostr(zahl1);
     edit2.text := inttostr(zahl2);
     aufgabenart := random(4);
     edit3.color := clwhite;
     edit3.text := '';

     if
       aufgabenart = 1
     then
         begin
              panel2.caption := '-';
         end;

     if
       aufgabenart = 2
     then
         begin
              panel2.caption := '*';
         end;

     if
       aufgabenart = 3
     then
         begin
              panel2.caption := '+';
         end;
              
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     randomize;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
          if
              (aufgabenart = 1) and
              (edit3.text = inttostr(zahl1-zahl2))
          then
              begin
                   panel5.caption := 'Richtig';
                   edit3.color := cllime;
              end
     else
          begin
               panel5.caption := 'Falsch';
               edit3.color := clred;
          end;

          if
            (aufgabenart = 2) and
            (edit3.text = inttostr(zahl1*zahl2))
          then
              begin
                   panel5.caption := 'Richtig';
                   edit3.color := cllime;
              end
          else
              begin
                   panel5.Caption := 'Falsch';
                   edit3.color := clred;
              end;

           if
            (aufgabenart = 3) and
            (edit3.text = inttostr(zahl1+zahl2))
          then
              begin
                   panel5.caption := 'Richtig';
                   edit3.color := cllime;
              end
          else
              begin
                   panel5.Caption := 'Falsch';
                   edit3.color := clred;
              end;
end;


end.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 08.05.07 16:10 
Für die Aufgabenart musst Du an dieser Stelle eine Case-Anweisung nehmen, bzw. die Frage nach dem Aufgabentyp ausklammern:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
if Aufgabentyp = 1 Then
BEgin
    If ... then
    Begin
        //Richtig
    end
    else
    begin
        //Falsch
    end;
end;



Case steht in der Delphi-Hilfe erklärt.

Außerdem ein Hinweis: Random fängt bei 0 an ... Ich möcht nicht das Opfer sein, was ständig Aufgaben vom Typ 0 lösen soll ...

P.S.: Das Debuggen hätte bei dieser Routine Problemlos gezeigt, warum es nicht geht ... Breakpoints und Step Into sind deine Freunde!!!

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 08.05.07 16:13 
Ah jetzt ja. Eine Insel. :mrgreen:

Punkt1: Was ist, wenn Aufgabenart = 0 ist?

Viel wichtger: Es wird zuerst der erste if-else Block ausgeführt. Danach wird der zweite if-else Block durchgeführt. Und dann der letzte. Und deswegen steht am Ende falsch.

Besser wäre hier soetwas:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
case aufgabentyp of
  1: if zahl1-zahl2 = StrToInt(Edit1.text) then "richtig" else "falsch"
  2: if zahl1+zahl2 = StrToInt(Edit1.text) then "richtig" else "falsch"
  //..
end;

_________________
We are, we were and will not be.
somebody Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 167
Erhaltene Danke: 2



BeitragVerfasst: Di 08.05.07 16:19 
Dann sagt der beim zweiten IF :'Deklaration erwartet, aber 'IF' gefunden.' Da ich noch nicht soviel erfahrung mit Delphi habe, frage ich mal

Wie behebt man das?

hier der 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:
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:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel2: TPanel;
    Panel4: TPanel;
    Edit1: TEdit;
    Panel5: TPanel;
    Button2: TButton;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  zahl2 : integer;
  zahl1 : integer;
  aufgabenart : integer;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
     zahl1 := random(10);
     zahl2 := random(10);
     edit1.text := inttostr(zahl1);
     edit2.text := inttostr(zahl2);
     aufgabenart := random(4);
     edit3.color := clwhite;
     edit3.text := '';

     if
       aufgabenart = 1
     then
         begin
              panel2.caption := '-';
         end;

     if
       aufgabenart = 2
     then
         begin
              panel2.caption := '*';
         end;

     if
       aufgabenart = 3
     then
         begin
              panel2.caption := '+';
         end;
              
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     randomize;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
          if
            aufgabenart = 1
          then
              begin
                   if
                     edit3.text = inttostr(zahl1-zahl2)
                   then
                        panel5.caption := 'Richtig';
                        edit3.color := cllime;
                   end
          else
              begin
                    panel5.caption := 'Falsch';
                    edit3.color := clred;
              end;
          end;

          if
            aufgabenart = 2
          then
              begin
                   if
                     edit3.text = inttostr(zahl1*zahl2)
                   then
                        panel5.caption := 'Richtig';
                        edit3.color := cllime;
                   end
          else
              begin
                    panel5.caption := 'Falsch';
                    edit3.color := clred;
              end;
          end;

          if
            aufgabenart = 3
          then
              begin
                   if
                     edit3.text = inttostr(zahl1+zahl2)
                   then
                        panel5.caption := 'Richtig';
                        edit3.color := cllime;
                   end
          else
              begin
                    panel5.caption := 'Falsch';
                    edit3.color := clred;
              end;
          end;
end;


end.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Di 08.05.07 16:24 
Schreib die Button2-Click Prozedur am besten mal ganz neu - am besten mit einer Case-Struktur. Da sind die begin..ends komplett durcheinander...und bei der Formatierung blickt da eh keiner durch ;-)

_________________
We are, we were and will not be.
Robinator
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 275

WinXP
BDS 2006
BeitragVerfasst: Di 08.05.07 16:25 
das behebt man, indem man begin und end an die richtigen stellen schreibt ;)

_________________
erare humanum est
somebody Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 167
Erhaltene Danke: 2



BeitragVerfasst: Di 08.05.07 16:59 
Ok, ich habe das jetzt gemacht und hab damit noch nie gearbeitet.

Bei mir sagt der dann :'Konstantenausdruck erwartet'

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
case
         aufgabenart
     of
       if zahl1-zahl2 = StrToInt(Edit1.text)
       then
           panel5.caption := 'Richtig'; //Hier sagt der das
           edit3.color := cllime;
       else panel5.caption := 'Falsch';
            edit3.color := clred;


Wie behebt man das?