Autor Beitrag
mitor
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Mo 01.11.04 14:25 
Hallo,

habe folgendes Problem: Ich will ein kleines Programm erstellen, welches mir bei Eingabe der Zahlen 1-6 den jeweiligen Wortlaut der Note in einem Edit-Fenster ausgibt (sprich bei Note 1 - Sehr Gut, Note 2 - Gut usw....).

Irgendwie haut das aber nicht ganz hin, Delphi schreit immer inkompatiblene Integer/String Typen...

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

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Edit1: TEdit;
    GroupBox3: TGroupBox;
    Label2: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var Note, Wortnote:integer;
begin
Note:=StrToInt(Edit1.Text);
Edit2.Text:=IntToStr(Wortnote);
case Note of
       1:Wortnote:='Sehr Gut';
       2:Wortnote:='Gut';
       3:Wortnote:='Befriedigend';
       4:Wortnote:='Ausreichend';
       5:Wortnote:='Mangelhaft';
       6:Wortnote:='Ungenügend';
else
         Wortnote:='Gibbet nich';
end;

end.
wulfskin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1349
Erhaltene Danke: 1

Win XP
D5 Pers (SSL), D2005 Pro, C, C#
BeitragVerfasst: Mo 01.11.04 14:27 
Hallo mitor,

Wortnote muss vom Typ String sein.

Gruß Hape!

_________________
Manche antworten um ihren Beitragszähler zu erhöhen, andere um zu Helfen.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 01.11.04 14:30 
Ein direkt mit den Notennamen initialisiertes String-Array wäre sicherlich auch keine schlechte Wahl, da man dann direkt mit dem Index drauf zugreifen könnte.
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Mo 01.11.04 14:38 
Also statt integer nur string hinpflanzen? Sorry bin noch recht neu....bisher nur mit Rechenops gearbeitet :(.
Kroni
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 720

Win 98, Win ME, Win2k, Win XP
D3 Pro
BeitragVerfasst: Mo 01.11.04 15:17 
und so wie dein code im moment ist, wird nichts bzw irgendein daten müll in edit2 geschrieben.
du solltest nach der case anweisung im edit2 die Variable reinschreibem, nachdem sie sehr gut, gut usw gespeichert hat....
dann müsste das Programm funktionieren.
und wenn wortnote ein string ist, musst du einfach edit2.text:=wortnote;
schreiben!
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Mo 01.11.04 15:45 
Ich bin zwar nicht der oberchecker aber dashier sollte eine musterlösung sein:

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:
procedure note;
var
   wortnote: array[1..6] of String;
begin
wortnote[1]:='Sehr gut';
wortnote[2]:='Gut';
wortnote[3]:='Befriedigend';
wortnote[4]:='Ausreichend';
wortnote[5]:='Ungenügend';
wortnote[6]:='Mangelhaft';
with Form1 do
begin
  try
    if (StrToInt(Edit1.Text)<=6) and (StrToInt(Edit1.Text)>=1) then
    begin
      Edit2.Text:=wortnote[StrToInt(Edit1.Text)];
    end else showmessage(Edit1.Text + ' ist ungültig!');
  except
    showmessage(Edit1.Text + ' ist ungültig!');
  end;
end;
end;


Ist getestet und funktioniert. Man könnte auch den Array in die Globalen Variablen packen, was speicherschonender ist, da man sie nur einmal bei FormCreate initialisieren muss. Ich wollte eben alles in eine procedure packen.
aufrufen kannst du sie zum Beispiel mit

ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button1Click(Sender: TObject);
begin
note;
end;


So das wars. :) Wenn du eine genaue Erklärung willst, frag ^^

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Mo 01.11.04 17:15 
Jetzt peil ich überhaupt nichts mehr. :(
Das ganze Dingens soll nur mit case umgesetzt werden, also nix mit if...das hätte ich glaub ich auch noch hinbekommen....aber mit dem case...
Kroni
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 720

Win 98, Win ME, Win2k, Win XP
D3 Pro
BeitragVerfasst: Mo 01.11.04 17:21 
hast du icq oder so?
ansonsten poste ich mal den korrekten Code:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure TForm1.Button1Click(Sender: TObject);
var Note:integer;
    wortnote:string; //Du musst dafür einen String benutzten, da du
                     //Buchstaben speicherst
begin
Note:=StrToInt(Edit1.Text);

case Note of
       1:Wortnote:='Sehr Gut';
       2:Wortnote:='Gut';
       3:Wortnote:='Befriedigend';
       4:Wortnote:='Ausreichend';
       5:Wortnote:='Mangelhaft';
       6:Wortnote:='Ungenügend';
else
         Wortnote:='Gibbet nich';
end;
edit2.text:=wortnote;  //Keine umwandlung des Datentyps notwendig, d a der Typ schon String ist

end;


Allerdings ist das Programm dann noch nicht abgesichert gegen falsche eingaben (Wie keine Eingabe in Edit1, Buchstabeneingabe in Edit1, aber das ist eine andere Geschichte)

Gruß, Kroni
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Mo 01.11.04 17:48 
Also erstmal danke. Das Problem bestand eben in der Deklaration der Variablen.
Nun türmt sich aber eine neue Frage auf:

"edit2.text:=wortnote;" steht jetzt nach dem Case. Wenn ich es mit zum begin (also unter Note:=StrToInt...) schreibe, klappt es nicht - why????
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Mo 01.11.04 17:48 
Also erstmal danke. Das Problem bestand eben in der Deklaration der Variablen.
Nun türmt sich aber eine neue Frage auf:

"edit2.text:=wortnote;" steht jetzt nach dem Case. Wenn ich es mit zum begin (also unter Note:=StrToInt...) schreibe, klappt es nicht - why???? Muss ich die Ausgabe immer nach der case Funktion schreiben?
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: Mo 01.11.04 17:57 
Das Programm muss der Variable Wortnote eben erst etwas zuweisen, das tut es während dem Case:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
case Note of   
       1:Wortnote:='Sehr Gut';   
       2:Wortnote:='Gut';   
       3:Wortnote:='Befriedigend';   
       4:Wortnote:='Ausreichend';   
       5:Wortnote:='Mangelhaft';   
       6:Wortnote:='Ungenügend';   
else   
         Wortnote:='Gibbet nich';   
end;

erst, wenn wortnote einen Inhalt hat, sollte man
Edit2:=wortnote;
ausführen! Befor das Case ausgeführt wurde, war die Variable einfach leer.
klar soweit? :D

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Mo 01.11.04 19:51 
Mkay...jetzt ist alles klar...

Hab' die ganze Sache nochmal bei 'nem anderen Sachverhalt angewandt...geht gleich besser, wenn man's gut erklärt bekommt :).

Wem's interessiert:
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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    auswahl: TRadioGroup;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    GroupBox3: TGroupBox;
    Label3: TLabel;
    Edit3: TEdit;
    GroupBox4: TGroupBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var zahla, zahlb, ergebnis:real;
begin
zahla:=StrToFloat(Edit1.Text);
zahlb:=StrToFloat(edit2.Text);

case auswahl.itemindex of
        0: ergebnis:=zahla + zahlb;
        1: ergebnis:=zahla - zahlb;
        2: ergebnis:=zahla / zahlb;
        3: ergebnis:=zahla * zahlb;
end;
edit3.Text:=FloatToStr(ergebnis);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.clear;
edit2.clear;
edit3.clear;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
close
end;

end.
Kroni
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 720

Win 98, Win ME, Win2k, Win XP
D3 Pro
BeitragVerfasst: Mo 01.11.04 19:58 
sry hätte ich dir auch schon vorher erklären können warum und wieso
Zitat:

du solltest nach der case anweisung im edit2 die Variable reinschreibem, nachdem sie sehr gut, gut usw gespeichert hat.

das sollte es eigentlich erklären, warum es nach der case anweisung stehen muss....sry
Aber wenn du es verstanden hast ist es ja super =)