读书人

帮写个函数调用解决办法

发布时间: 2012-03-24 14:00:46 作者: rapoo

帮写个函数调用

帮写个函数调用

IY 可能等于1 - NNNN
listbox2.Items[N]行 每行都是数字
if iy=0 then ww:=0;
if iy=1 then ww:=StrToInt(listbox2.Items[0]);
if iy=2 then ww:=StrToInt(listbox2.Items[0])+StrToInt(listbox2.Items[1]);
if iy=3 then ww:=StrToInt(listbox2.Items[0])+StrToInt(listbox2.Items[1])+StrToInt(listbox2.Items[2]);
if iy=4 then ww:=StrToInt(listbox2.Items[0])+StrToInt(listbox2.Items[1])+StrToInt(listbox2.Items[2])+StrToInt(listbox2.Items[3]);
if iy=5 then ww:=StrToInt(listbox2.Items[0])+StrToInt(listbox2.Items[1])+StrToInt(listbox2.Items[2])+StrToInt(listbox2.Items[3])+StrToInt(listbox2.Items[4]);
if iy=6 then ww:=StrToInt(listbox2.Items[0])+StrToInt(listbox2.Items[1])+StrToInt(listbox2.Items[2])+StrToInt(listbox2.Items[3])+StrToInt(listbox2.Items[4])+StrToInt(listbox2.Items[5]);
if iy=7 then ww:=StrToInt(listbox2.Items[0])+StrToInt(listbox2.Items[1])+StrToInt(listbox2.Items[2])+StrToInt(listbox2.Items[3])+StrToInt(listbox2.Items[4])+StrToInt(listbox2.Items[5])+StrToInt(listbox2.Items[6]);

[解决办法]
var i : inetger;
begin
Result := 0;
for i := 0 to iy - 1 do
begin
Result := Result + strtoint(listbox2.items[i]);
end;
end;
[解决办法]

Delphi(Pascal) code
function CalcSum(iy: Integer): Double;var  i: Integer;begin  Result := 0;  if iy > 0 then    for i := 0 to iy - 1 do      Result := Result + StrToInt(ListBox2.Items[i]);end;
[解决办法]
哈哈,被稻草人占先了,补一个递归版本
Delphi(Pascal) code
function CalcSum2(iy: Integer): Double;begin  if iy = 0 then    Result := 0   else    Result := StrToInt(ListBox2.Items[iy-1]) + CalcSum2(iy-1);end; 

读书人网 >.NET

热点推荐