菜鸟求助(代码改错)

huangxin89 2010-04-22 07:30:04
做的是一个日期计算的,比如2008年9月8日和2009年4月4日相差多少年多少月多少日。
就有一个"计算日期"按钮。
但是小弟编的代码错误百出,请高手指正,很感谢
下面是代码:
unit work1;

interface

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

type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Button1: TButton;
Edit7: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

var
date1:string;
date2:string;
temp1:string;
temp2:string;
temp3:string;
day1:integer;
day2:integer;
y1:integer;
y2:integer;
m1:integer;
m2:integer;
d1:integer;
d2:integer;
n:integer;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
date1:=edit1.text+edit2.text+edit3.text;
date2:=edit4.text+edit5.text+edit6.text;
y1:=strtoint(edit1.text);y2:=strtoint(edit4.text);
m1:=strtoint(edit2.text);m2:=strtoint(edit5.text);
d1:=strtoint(edit3.text);d2:=strtoint(edit6.text);
day1:=strtoint(date1);day2:=strtoint(date2);
if (y1 mod 400 =0) or ((y1 mod 100<>0) and (y1 mod 4=0)) then
begin
n:=1;
end
else
n:=2;
end ;
if day1 <= day2 then
begin
if(m1<=m2) then
begin
if(d1<=d2) then
begin
temp1:=inttostr(y2-y1);
temp2:=inttostr(m2-m1);
temp3:=inttostr(d2-d1);
end
else
temp1:=inttostr(y2-y1);
temp2:=inttostr(m2-m1-1);
case m1 of
1: temp3:=inttostr(d2-d1+31);
2: if n=1 then
begin
temp3:=inttostr(d2-d1+29);
end
else
temp3:=inttostr(d2-d1+28);
end;
3: temp3:=inttostr(d2-d1+31);
4: temp3:=inttostr(d2-d1+30);
5: temp3:=inttostr(d2-d1+31);
6: temp3:=inttostr(d2-d1+30);
7: temp3:=inttostr(d2-d1+31);
8: temp3:=inttostr(d2-d1+31);
9: temp3:=inttostr(d2-d1+30);
10: temp3:=inttostr(d2-d1+31);
11: temp3:=inttostr(d2-d1+30);
12: temp3:=inttostr(d2-d1+31);
end;
end
else
if(d1<=d2) then
begin
temp1:=inttostr(y2-y1-1);
temp2:=inttostr(m2-m1+12);
temp3:=inttostr(d2-d1);
end
else
temp1:=inttostr(y2-y1-1);
temp2:=inttostr(m2-m1+11);
case m1 of
1: temp3:=inttostr(d2-d1+31);
2: if n=1 then
begin
temp3:=inttostr(d2-d1+29);
end
else
temp3=inttostr(d2-d1+28);
end;
3: temp3:=inttostr(d2-d1+31);
4: temp3:=inttostr(d2-d1+30);
5: temp3:=inttostr(d2-d1+31);
6: temp3:=inttostr(d2-d1+30);
7: temp3:=inttostr(d2-d1+31);
8: temp3:=inttostr(d2-d1+31);
9: temp3:=inttostr(d2-d1+30);
10: temp3:=inttostr(d2-d1+31);
11: temp3:=inttostr(d2-d1+30);
12: temp3:=inttostr(d2-d1+31);
end;
end;
end
else
application.messagebox('前后日期次序颠倒','错误');
exit;
end;
edit7.text:=temp1+'年'+temp2+'月'+temp3+'日'

end;

end.

提示的错误有:
[Error] work1.pas(73): Declaration expected but 'IF' found
[Error] work1.pas(95): Undeclared identifier: '3'
[Error] work1.pas(96): Undeclared identifier: '4'
[Error] work1.pas(97): Undeclared identifier: '5'
[Error] work1.pas(98): Undeclared identifier: '6'
[Error] work1.pas(99): Undeclared identifier: '7'
[Error] work1.pas(100): Undeclared identifier: '8'
[Error] work1.pas(101): Undeclared identifier: '9'
[Error] work1.pas(102): Undeclared identifier: '10'
[Error] work1.pas(103): Undeclared identifier: '11'
[Error] work1.pas(104): Undeclared identifier: '12'
[Error] work1.pas(107): '.' expected but 'ELSE' found
[Error] work1.pas(109): Identifier redeclared: 'Finalization'
[Warning] work1.pas(114): Text after final 'END.' - ignored by compiler
[Fatal Error] pro1.dpr(5): Could not compile used unit 'work1.pas'
...全文
113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangxin89 2010-04-24
  • 打赏
  • 举报
回复
谢谢大家,刚弄出来了,就是嵌套出了些错误
tity 2010-04-23
  • 打赏
  • 举报
回复
把case
2: if n=1 then
begin
temp3:=inttostr(d2-d1+29);
end
else
temp3:=inttostr(d2-d1+28);
end;
里面的代码用begin end括起来
柯本 2010-04-23
  • 打赏
  • 举报
回复
所有有else都多了end;改好至少可以编译的
begin

date1:=edit1.text+edit2.text+edit3.text;
date2:=edit4.text+edit5.text+edit6.text;
y1:=strtoint(edit1.text);y2:=strtoint(edit4.text);
m1:=strtoint(edit2.text);m2:=strtoint(edit5.text);
d1:=strtoint(edit3.text);d2:=strtoint(edit6.text);
day1:=strtoint(date1);day2:=strtoint(date2);
if (y1 mod 400 =0) or ((y1 mod 100<>0) and (y1 mod 4=0)) then
begin
n:=1;
end
else
n:=2;
// end ; 这里多end;
if day1 <= day2 then
begin
if(m1<=m2) then
begin
if(d1<=d2) then
begin
temp1:=inttostr(y2-y1);
temp2:=inttostr(m2-m1);
temp3:=inttostr(d2-d1);
end
else
temp1:=inttostr(y2-y1);
temp2:=inttostr(m2-m1-1);
case m1 of
1: temp3:=inttostr(d2-d1+31);
2: if n=1 then
begin
temp3:=inttostr(d2-d1+29);
end
else
temp3:=inttostr(d2-d1+28);
// end; 这里多end;
3: temp3:=inttostr(d2-d1+31);
4: temp3:=inttostr(d2-d1+30);
5: temp3:=inttostr(d2-d1+31);
6: temp3:=inttostr(d2-d1+30);
7: temp3:=inttostr(d2-d1+31);
8: temp3:=inttostr(d2-d1+31);
9: temp3:=inttostr(d2-d1+30);
10: temp3:=inttostr(d2-d1+31);
11: temp3:=inttostr(d2-d1+30);
12: temp3:=inttostr(d2-d1+31);
end;
end
else
if(d1<=d2) then
begin
temp1:=inttostr(y2-y1-1);
temp2:=inttostr(m2-m1+12);
temp3:=inttostr(d2-d1);
end
else
temp1:=inttostr(y2-y1-1);
temp2:=inttostr(m2-m1+11);
case m1 of
1: temp3:=inttostr(d2-d1+31);
2: if n=1 then
begin
temp3:=inttostr(d2-d1+29);
end
else
temp3:=inttostr(d2-d1+28); ///这个=应该是:=
//end; 这里多end;
3: temp3:=inttostr(d2-d1+31);
4: temp3:=inttostr(d2-d1+30);
5: temp3:=inttostr(d2-d1+31);
6: temp3:=inttostr(d2-d1+30);
7: temp3:=inttostr(d2-d1+31);
8: temp3:=inttostr(d2-d1+31);
9: temp3:=inttostr(d2-d1+30);
10: temp3:=inttostr(d2-d1+31);
11: temp3:=inttostr(d2-d1+30);
12: temp3:=inttostr(d2-d1+31);
// end; 这里多end;
end;
end
else
application.messagebox('前后日期次序颠倒','错误');
exit;
// end; 这里多end;
edit7.text:=temp1+'年'+temp2+'月'+temp3+'日'

end;

你的else没有begin,就不要end;
sg_knight 2010-04-23
  • 打赏
  • 举报
回复
DaysBetween(const ANow, AThen: TDateTime): Integer;
没必要自己写的麻烦。
cloudysoul 2010-04-23
  • 打赏
  • 举报
回复
做的是一个日期计算的,比如2008年9月8日和2009年4月4日相差多少年多少月多少日。
时间比较的方法很多,没见过这么复杂的。。。。。
再百度一下吧、、、、
huangxin89 2010-04-23
  • 打赏
  • 举报
回复
各位按你们说的做了,可是还是错误提示
wliaoc 2010-04-23
  • 打赏
  • 举报
回复
if (y1 mod 400 =0) or ((y1 mod 100<>0) and (y1 mod 4=0)) then
begin
n:=1;
end
else
n:=2;
end ;
如果没看错,这里应该多了
李_军 2010-04-23
  • 打赏
  • 举报
回复
delphi在DateUtils单元中有个现成的函数
DaysBetween(const ANow, AThen: TDateTime): Integer;

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧