請幫忙看段代碼出錯在哪里呢﹖(代碼)(10分)

  • 主题发起人 主题发起人 stonehuang
  • 开始时间 开始时间
S

stonehuang

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm12.suiEdit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
count_CC1:integer;
TempInt:real;
begin
if key=13 then
begin
if dm.ADOTable_CC1.Active then
count_CC1:=dm.ADOTable_CC1.RecordCount;
dm.ADOTable_CC1.Append;
dm.ADOTable_CC1.FieldByName('ID').AsString:=inttostr(count_CC1+1);
dm.ADOTable_CC1.FieldByName('JQID').AsString:=dm.ADOTable_Machine.FieldValues['ID'];
dm.ADOTable_CC1.FieldByName('CPID').AsString:=dm.ADOTable_CP.FieldValues['CPID'];
dm.ADOTable_CC1.FieldByName('Date').AsDateTime:=form10.DateTimePicker1.Date;
dm.ADOTable_CC1.FieldByName('BanCi').AsString:=form10.ComboBox3.Items[form10.ComboBox3.Itemindex];
dm.ADOTable_CC1.FieldByName('Name').AsString:=suicombobox1.Items[suicombobox1.Itemindex];
dm.ADOTable_CC1.FieldByName('Times').AsFloat:=strtofloat(suiedit1.Text);
dm.ADOTable_CC1.Post;
dm.ADOTable_CC1.Refresh;
end;
TempInt:=StrTofloat(suiedit1.text);
sum:=sum+TempInt;
form10.suiEdit10.Text:=floattostr(sum);
end;
例如輸入30,但傳給form10.suiedit10.text的值為33﹐而實際數据庫裏又為30﹐請問出錯在哪里呢﹖
 
会不会是
TempInt:=StrTofloat(suiedit1.text);
sum:=sum+TempInt;
form10.suiEdit10.Text:=floattostr(sum);
sum 的初值问题,例如, sunm 一开始为 3 然后你再输入 30 那不就变成33 了啊
 
你的代码运行的时候没出错吗?我认为什么出错的.
你刚按错时StrTofloat(suiedit1.text);这里应该会报错.

你把它直接放keyup里呀
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
count_CC1:integer;
TempInt,sum:real;
begin
if key=13 then
if dm.ADOTable_CC1.Active then begin
count_CC1:=dm.ADOTable_CC1.RecordCount;
dm.ADOTable_CC1.Append;
dm.ADOTable_CC1.FieldByName('ID').AsString:=inttostr(count_CC1+1);
dm.ADOTable_CC1.FieldByName('JQID').AsString:=dm.ADOTable_Machine.FieldValues['ID'];
dm.ADOTable_CC1.FieldByName('CPID').AsString:=dm.ADOTable_CP.FieldValues['CPID'];
dm.ADOTable_CC1.FieldByName('Date').AsDateTime:=form10.DateTimePicker1.Date;
dm.ADOTable_CC1.FieldByName('BanCi').AsString:=form10.ComboBox3.Items[form10.ComboBox3.Itemindex];
dm.ADOTable_CC1.FieldByName('Name').AsString:=suicombobox1.Items[suicombobox1.Itemindex];
dm.ADOTable_CC1.FieldByName('Times').AsFloat:=strtofloat(suiedit1.Text);
dm.ADOTable_CC1.Post;
dm.ADOTable_CC1.Refresh;
end;

TempInt:=StrTofloat(suiedit1.text);
sum:=sum+TempInt;
form10.suiEdit10.Text:=floattostr(sum);
end;
 
是sum 的初值问题,找个合适的地方给sum赋初值吧
 
dm.ADOTable_CC1.FieldByName('BanCi').AsString:=form10.ComboBox3.Items[form10.ComboBox3.Itemindex];
应改为
dm.ADOTable_CC1.FieldByName('BanCi').AsString:=form10.ComboBox3.text;
更直观些,而且效率更好点。
 
另外,你出错的原因:
你是先把suiedit1.Text的值写入数据库后,才给suiedit1.Text赋值的
 
对,应该是sum初值的问题,你试一下。
procedure TForm12.suiEdit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
count_CC1:integer;
TempInt,sum:real;
begin
sum:=0;
if key=13 then
begin
if dm.ADOTable_CC1.Active then
count_CC1:=dm.ADOTable_CC1.RecordCount;
dm.ADOTable_CC1.Append;
dm.ADOTable_CC1.FieldByName('ID').AsString:=inttostr(count_CC1+1);
dm.ADOTable_CC1.FieldByName('JQID').AsString:=dm.ADOTable_Machine.FieldValues['ID'];
dm.ADOTable_CC1.FieldByName('CPID').AsString:=dm.ADOTable_CP.FieldValues['CPID'];
dm.ADOTable_CC1.FieldByName('Date').AsDateTime:=form10.DateTimePicker1.Date;
dm.ADOTable_CC1.FieldByName('BanCi').AsString:=form10.ComboBox3.Items[form10.ComboBox3.Itemindex];
dm.ADOTable_CC1.FieldByName('Name').AsString:=suicombobox1.Items[suicombobox1.Itemindex];
dm.ADOTable_CC1.FieldByName('Times').AsFloat:=strtofloat(suiedit1.Text);
dm.ADOTable_CC1.Post;
dm.ADOTable_CC1.Refresh;
end;
TempInt:=StrTofloat(suiedit1.text);
sum:=sum+TempInt;
form10.suiEdit10.Text:=floattostr(sum);
end;

 
procedure TForm12.FormCreate(Sender: TObject);
begin
sum:=0﹔
end;
已經賦值呢
各位大哥﹐問題依然沒解決﹗
 
我用的是下拉框選擇﹐如果不先寫入數据庫﹐那么數据庫的紀錄﹐如果我本來輸入一條﹐就出現了下拉框里面多少個數的紀錄﹐所以先寫入才沒呢
 
你自己调试以下,可能你的代码都没进去
 
private
sum:real;
我還在這里有﹗是否不行﹖
 
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
count_CC1:integer;
TempInt,sum:real;
begin
if key=13 then
if dm.ADOTable_CC1.Active then begin
count_CC1:=dm.ADOTable_CC1.RecordCount;
dm.ADOTable_CC1.Append;
dm.ADOTable_CC1.FieldByName('ID').AsString:=inttostr(count_CC1+1);
dm.ADOTable_CC1.FieldByName('JQID').AsString:=dm.ADOTable_Machine.FieldValues['ID'];
dm.ADOTable_CC1.FieldByName('CPID').AsString:=dm.ADOTable_CP.FieldValues['CPID'];
dm.ADOTable_CC1.FieldByName('Date').AsDateTime:=form10.DateTimePicker1.Date;
dm.ADOTable_CC1.FieldByName('BanCi').AsString:=form10.ComboBox3.Items[form10.ComboBox3.Itemindex];
dm.ADOTable_CC1.FieldByName('Name').AsString:=suicombobox1.Items[suicombobox1.Itemindex];
dm.ADOTable_CC1.FieldByName('Times').AsFloat:=strtofloat(suiedit1.Text);
dm.ADOTable_CC1.Post;
dm.ADOTable_CC1.Refresh;
TempInt:=StrTofloat(suiedit1.text);
sum:=sum+TempInt;
form10.suiEdit10.Text:=floattostr(sum);
end;

end;
 
接受答案了.
 
TempInt:=StrTofloat(suiedit1.text);
sum:=sum+TempInt;
form10.suiEdit10.Text:=floattostr(sum);

逻辑错误!!!
分析:初始:sum=0
按下3:执行sum=0+3=3
接着按下0:又执行sum=3+30=33
。。。
这样每按一键都在累加,严重错误!!!!
 
后退
顶部