procedure TForm1.Button1Click(Sender: TObject);
var
i,x,w,y:integer;
begin
y:=0;
x:=strtoint(edit1.text);
for i:=1 to x do
begin
w:=x*(x-i);
y:=y+w;
i:=i+1;
end;
edit2.Text:=inttostr(w);
end;end.出現這樣的錯誤!
Assignment to For-loop Variable 'i'.
var
i,x,w,y:integer;
begin
y:=0;
x:=strtoint(edit1.text);
for i:=1 to x do
begin
w:=x*(x-i);
y:=y+w;
i:=i+1;
end;
edit2.Text:=inttostr(w);
end;end.出現這樣的錯誤!
Assignment to For-loop Variable 'i'.
中是不允许改变循环变量的
就是说你的i是不能更改的,你只需要再声明一个变量使用就是了
var
i,x,w:integer;
begin
w:=1;
x:=strtoint(edit1.text);
for i:=1 to x do
begin
w:=i*w;
end;
edit1.Text:=inttostr(w);end;end.
begin
if n<=1 then result:=1 else
result:=n*jc(n-1);
end;
x,w:integer;
begin
w:=1;
for x:=strtoint(edit1.text) downto 1 do
begin
w:=w*x;
end;
edit2.Text:=inttostr(w);
end;
function factorial(x:integer):integer;
begin
if x <> 1 then
result:=x*factorial(x-1)
else
result:=1;
end;
delphi中没有一种变量,能存那么大的数
100!的结果光最后的“0”就有24个,所以算到一定大的时候就不能算了所以要用一种算法——高精度计算!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
RichEdit1: TRichEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;function GetMultiplication(ANum1: integer; ANum2: string): string;
//功能:得到乘法结果
//参数:ANum1,因数1,一位整数;
// ANum2,因数2,多位整数字符串
//返回:多位整数字符串function GetFactorial(ANum: integer): string;
//功能:得到阶乘结果
//参数:ANum,做阶乘运算的数
//返回:多位整数字符串implementation{$R *.dfm}function GetMultiplication(ANum1: integer; ANum2: string): string;
var
VIndex: integer; //循环变量 //计算所用临时变量
VCurrentBit: integer; //当前运算位
VCarryBit: integer; //进位
begin
//初始化
VCarryBit := 0;
Result := ''; //逐位运算
for VIndex := Length(ANum2) downto 1 do
begin
VCurrentBit := StrToInt(Copy(ANum2, VIndex, 1)); //取运算位
VCurrentBit := VCurrentBit * ANum1 + VCarryBit; //运算
VCarryBit := VCurrentBit div 10; //取进位
VCurrentBit := VCurrentBit mod 10; //取当前位
Result := IntToStr(VCurrentBit) + Result; //加入结果
end;//for
if VCarryBit > 0 then
Result := IntToStr(VCarryBit) + Result; //加上最后的进位
end;function GetFactorial(ANum: integer): string;
begin
if ANum = 0 then
Result := '1'
//0!=1
else
Result := GetMultiplication(ANum, GetFactorial(ANum - 1));
//n!=n*(n-1)!
end;procedure TForm1.Button1Click(Sender: TObject);
begin
RichEdit1.Text := GetFactorial(100); //求100!
end;end.