Autor Beitrag
DelphiDelphin
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Mo 07.03.05 13:46 
Hallo Zusammen Ich Möchte Einen Durchschnitts Berechner Programmieren Hab Aber 0 Ahnung wie Ich dass Machen Muss.
Ich Habe 10. Felder Dort Kann Mann Dann die Zahlen Angeben Wenn Alle Felder Ausgefühlt Sind müsste Man Ja Folgende Rechnung In Delphi Eingeben:
(A+B+C+D+E+F+G+H+I+J )/10
Aber Es Sind Ja Nicht Immer 10 Felder Ausgefüllt Vielleicht Füllt Jemand Nur 5 Felder Aus Und In Den Anderen Steht 0 Dann Soll Es Also nur
(A+B+C+D+E)/10
Rechnen Wie Ist Dass Möglich.

Mit freundlichen Grüßen

DelphiDelphin

Ps: herzlichen Glückwunsch Zu 1000 Tagen Delphi Forum :party:
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mo 07.03.05 13:48 
Nimme eine Schleife und zähle die Felder, dessen Inhalt ungleich 0 ist.
DelphiDelphin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Mo 07.03.05 14:48 
Ich Nur Verstehen Bahnhof :roll:
Kannst Du das Auch noch mal ausführlich Für einen Noob Erklähren
:flehan:
Karlson
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Mo 07.03.05 14:52 
Wo wir grad beim Verstehen von Bahnhof sind. Hast du schonmal was davon gehört dass man im Deutschen nicht jedes Wort grossschreibt? :mrgreen: ;)

Du hast 10 Felder (wohl Edits).
Diese heissen Edit1...edit10.

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:
function durchschnitt:extended;
var i,teiler : integer;
    Ed : TEdit;
    CurrVal : integer;
begin
CurrVal := 0;
Teiler := 0;
 for i := 1 to 10 do
  begin
   Ed := TEdit(FindComponent('Edit' + inttostr(i))); 
   //sucht das Editfeld mit dem Namen 'edit' + i, wobei i die Werte 1 bis 10 nacheinander annimmt
   if ed.text <> '0' then
   //wenn der Inhalt des oben gefundenen Edit Felds ungleich '0' ist
    begin
     inc(currval, strtoint(ed.text));
     //dann zähle den inhalt des Edit-Felds zu der Variable Currval hinzu
     inc(teiler);
     // und erhöhe die Variable Teiler um 1
    end;
  end;
 result := Currval / teiler;
 //Zu guter letzt teilst du die Currvalvariable durch die teiler
end;


Moderiert von user profile iconKarlson: Fehler korrigiert


Zuletzt bearbeitet von Karlson am Mo 07.03.05 15:57, insgesamt 2-mal bearbeitet
DelphiDelphin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Mo 07.03.05 15:13 
Was Mach Ich Falsch :bawling:
Bei Mir Wird Immer Die Zeile Mit dem
end.
Rot Markiert. Hier Mein Ganzer 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:
 unit Unit3;

interface

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

type
  TForm3 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
function durchschnitt:extended;
var i,teiler : integer;  
    Ed : TEdit;  
    CurrVal : integer;  
begin  
CurrVal := 0;  
Teiler := 0;  
 for i := 1 to 10 do  
  begin
   Ed := TEdit(FindComponent('Edit' + inttostr(i)));  
   if ed.text <> '0' then  
    begin  
     inc(currval, strtoint(edit1.text));  
     inc(teiler);  
    end;  
  end;  
 result := Currval / teiler;  
end;





end.

DHX 8)
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mo 07.03.05 15:24 
Da du die Funktion innerhalb der Procedure definiert hast, fehlt dir nach der Funktion noch
ausblenden Delphi-Quelltext
1:
2:
3:
begin
  ShowMessage('Durchschnitt: ' + FloatToStr(Durchschnitt));
end;
DelphiDelphin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Mo 07.03.05 15:39 
Hm Jetzt Kommt Zwar Kein Fehler Mehr Aber Wen Ich In Das Oberste Feld 45 eingebe und in das Zweite 12 Dan Kommt Durchschnitt 45 :shock:
Wenn ich In das oberste Feld 10 Eingebe und In das Zweite Kommt Durchschnitt 10
:shock:
Karlson
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Mo 07.03.05 15:41 
Der Ansatz steht da. Das Problem wirste wohl selbst lösen können...
DelphiDelphin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Mo 07.03.05 15:50 
Juhui Ich Hab’s Geschafft :party:
Hi Falls Jemand Zeit Hat würd Ich es Toll Finden Wen Mir Jemand Dass Ganze Noch Erklären Könnte weil Hab Jetzt Alles Nur Abgeschrieben und Keine Ahnung wie Dass Ganze Funktioniert und So werde ich Ja nicht viel Dabei Lernen .

Big DHX :flehan:
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mo 07.03.05 15:54 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
function durchschnitt:extended; // Was eine Funktion ist, solltest du wissen
var i,teiler : integer;
    Ed : TEdit;
    CurrVal : integer;
begin
CurrVal := 0; // Initialisierung des Summen-Wertes
Teiler := 0; // Init des Teilers
 for i := 1 to 10 do // Schleife über die Eingabefelder
  begin
   Ed := TEdit(FindComponent('Edit' + inttostr(i))); // Eingabefeld feststellen
   if ed.text <> '0' then // falls Eingabe <> null im Feld i
    begin
     inc(currval, strtoint(ed.text)); // Den Summen-Wert um den Eingabewert erhöhen
     inc(teiler); // Da ungleich null, muss der Teiler erhöht werden.
    end;
  end;
 result := Currval / teiler; // Funktions-Ergebnis berechnen
end;


Zu allen anderen Dingen sollte die die OH von Delphi genügen.
DelphiDelphin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Mo 07.03.05 16:17 
Vielen Dank :dance:
DelphiDelphin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Sa 12.03.05 11:31 
Hi Hallo es ist noch ein Problem abgetaucht der Durchschnitts berechner ist untauglich mit diesem Quelltext
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:
procedure TForm3.Button1Click(Sender: TObject);
function Schnitt:extended;
var
i: Integer;
teiler : Integer;
Edit: TEdit;
Zahl2: Integer;
begin
Zahl2:= 0;
teiler:=0;
for i := 1 to 15 do
begin
Edit := TEdit(FindComponent('edit'+ inttostr(i)));
if Edit.Text <> '0' then
begin
inc(Zahl2, strtoint(Edit.Text));
inc(teiler);
end;
end;
result:= Zahl2/teiler;
end;
begin
Panel1.Caption:= Floattostr(schnitt);
end;
end.

Denn es werden keine Komma werte angenommen zb 2.75
Hab schon versucht alle Integer Sachen durch extended und StrtoFloat ersetzen aber hoffnungslos hat jemand hilfe???
Karlson
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Sa 12.03.05 11:38 
Zahl2 ist ein Integer-Wert.
Variablen vom Typ Integer können nur mit ganzen Zahlen rechnen.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
var
 zahl2 : extended;

[...]

Zahl2 := Zahl2 + strtofloat(edit.text);

[...]
DelphiDelphin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Sa 12.03.05 11:48 
Hi vielen Dank

Aber wie geht den dass

Logisch wäre doch so
ausblenden Delphi-Quelltext
1:
Inc (zahl2,  StrtoFloat (Edit.Text));					

Wiso denn so ??
ausblenden Delphi-Quelltext
1:
Zahl2 := Zahl2 + strtofloat(edit.text);					

Hi bin halt noob drum würde es mich wunder nehmen :roll:
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 12.03.05 11:49 
user profile iconDelphiDelphin hat folgendes geschrieben:
Logisch wäre doch so
ausblenden Delphi-Quelltext
1:
Inc (zahl2,  StrtoFloat (Edit.Text));					

Wäre es nicht, denn Inc geht nur für Integer ;)

AXMD
DelphiDelphin Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 181



BeitragVerfasst: Sa 12.03.05 12:13 
A Ha vielen Dank auch ein noob hat was gerafft :lol:
Aber für was stet überhaupt Inc ( was kann man sonst noch damit machen ) :nixweiss:
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 12.03.05 12:18 
Wie wärs wenn du mal einen Blick in die OH wirfst :roll:? Inc=Increment=Erhöhen

AXMD
Pr0g
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 220


D7 Pers
BeitragVerfasst: Sa 12.03.05 12:20 
user profile iconDelphiDelphin hat folgendes geschrieben:
Aber für was stet überhaupt Inc ( was kann man sonst noch damit machen ) :nixweiss:

Guck doch mal in die Hilfe.

Mit Inc() kannst du einen Variable um eins erhöhen:
ausblenden Delphi-Quelltext
1:
Inc(Variable);					

Optional lässt sich auch ein zweiter Parameter angeben, dann wird die Variable nicht um eins, sondern um den Wert des Parameters erhöht, also in diesem Beispiel um 5:
ausblenden Delphi-Quelltext
1:
Inc(Variable, 5);					


Der Gegenbefehl zu Inc() ist Dec(), dieser wird genauso wie Inc() genutzt, nur dass er die Variable verringert.

MfG Pr0g