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

WinXP Home
D 6.0 PE
BeitragVerfasst: Mo 13.12.04 20:38 
Hallo,

hab' ein kleines Problem mit der Berechnung von Fakultäten...

Das Prog an sich startet ja, bloß kommt im Ausgabefesnter im die selbe Zahl...könntet ihr mal gucken wo der Fehler is...

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

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    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 i, zahl, result:integer;
begin
if zahl < 0
  then
      result:=-1
  else
      begin
      result:=1;
      for i:=zahl to 1 do
          result:=result * i;
end;
edit2.Text:=inttostr(result);
end;
end.
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Mo 13.12.04 20:45 
Hallo,
Dein Problem ist, das Dein Code nur einmal durchlaufen wird.
Versuch es mal mit den Befehlen while, until, repeat und co.
Viel Glück, hoffe Du kommst selbst auf einen richtigen Code.
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Di 14.12.04 17:40 
Hab's nochmal überarbeitet...leider kommt jetzt wieder nur Schnasche raus :(. Irgendwie rechnet der jetzt nur die Zahl ins Quadrat ^^....

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

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var zahl, ergebnis, i:integer;
begin
zahl:=StrToInt(edit1.Text);
if zahl <= 1
  then
      ergebnis:=1
  else
      if zahl = 2
        then
            ergebnis:=2
        else
            begin
            for i:=1 to zahl do
            ergebnis:= zahl * i
            end;
edit2.text:=IntToStr(ergebnis);
end;
end.
DeCodeGuru
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1333
Erhaltene Danke: 1

Arch Linux
Eclipse
BeitragVerfasst: Di 14.12.04 17:56 
Sry, wenn ich mir deinen Code nicht genau angeschaut habe, aber guck dir mal folgenden Code an...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.Button1Click(Sender: TObject);
var
  i, n, res: Integer;
begin
  n := 3; //n := StrToInt(EditX.Text)
  if n < 1 then
    raise Exception.Create('Bitte Zahl >= 1 eingeben!')
  else
  begin
    res := 1;
    for i := n downto 1 do
      res := res * i;
  end;
  ShowMessage(IntToStr(res));
end;

_________________
Viele Grüße
Jakob
mitor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 35

WinXP Home
D 6.0 PE
BeitragVerfasst: Di 14.12.04 18:07 
Boooaaahhh...endlich geht's....

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

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var zahl, result, i:integer;
begin
n:=StrToInt(edit1.Text);

if zahl< 1
  then
      raise Exception.Create('Bitte Zahl >= 1 eingeben!')
  else
      begin
           result:=1;
           for i:=zahl downto 1 do
           result:= result * i;
           end;
edit2.text:=IntToStr(result);
end;
end.
Tilo
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 1098
Erhaltene Danke: 13

Win7 geg. WInXP oder sogar Win98
Rad2007
BeitragVerfasst: Di 14.12.04 21:03 
Glückwunsch :dance: