以下是代码,是一个计算生产周期的小程序,第一次用DELPHI,出了好多问题解决不了,请教大侠帮助。unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, ExtCtrls, ValEdit;type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    BitBtn2: TBitBtn;
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Edit3: TEdit;
    StringGrid1: TStringGrid;
    Edit4: TEdit;
    Edit5: TEdit;
    Button2: TButton;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    RadioGroup1: TRadioGroup;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);begin
StringGrid1.RowCount:=StrToInt(Edit1.Text);//得到表格的行数end;
procedure TForm1.Button2Click(Sender: TObject);
var m,t:integer;gtime:array[1..10]of integer;begin//输入每个工序的时间
     for m:=1 to 10 do
     gtime[m]:=0;
     StringGrid1.Options:=StringGrid1.Options+[goediting];
     m:=StrToInt(Edit4.text)-1;
     StringGrid1.Cells[0,m]:=Edit4.Text;
     StringGrid1.Cells[1,m]:=Edit5.Text;
     t:=StrToInt(Edit5.text);
     gtime[m+1]:=t;
     end;   procedure TForm1.RadioGroup1Click(Sender: TObject);
var max,n,m,i,gnum,lnum,sum:integer;gtime:array[1..10]of integer;
begin//用三种方式计算工程时间
sum:=0;
gnum:=StrToInt(Edit1.text);
lnum:=StrToInt(Edit3.text);
case RadioGroup1.ItemIndex of
0:n:=1;
1:n:=2;
2:n:=3;
end;
if n=1 then
begin
   for i:=1 to gnum do
       sum:=sum+gtime[i]*lnum;
end;if n=2 then
begin
max:=0;
for i:=1 to gnum do
  begin
      if gtime[i]>max then
         max:=gtime[i];
      sum:=sum+gtime[i];
  end;
     sum:=sum+(lnum-1)*max;
 end;
if n=3 then
begin
 sum:=gtime[1]*lnum;
   for i:=2 to gnum do
    begin
     if gtime[i]>gtime[i-1] then
        sum:=sum-gtime[i-1]*(lnum-1)+gtime[i]*lnum;
     if gtime[i]<=gtime[i-1] then
        sum:=sum+gtime[i];
    end;
   end;
  Edit2.text:=inttostr(sum);
end;
end.

解决方案 »

  1.   

    procedure TForm1.Button2Click(Sender: TObject);
    var m,t:integer;gtime:array[1..10]of integer;begin
         for m:=1 to 10 do
         gtime[m]:=0;
         StringGrid1.Options:=StringGrid1.Options+[goediting];
         m:=StrToInt(Edit4.text)-1;
         StringGrid1.Cells[0,m]:=Edit4.Text;
         StringGrid1.Cells[1,m]:=Edit5.Text;
         t:=StrToInt(Edit5.text);
         gtime[m+1]:=t;  //////  这里当 m=10 时,超出数组下标了
      end;gnum:=StrToInt(Edit1.text);//// 这里应该保证 gnum 在 1 到 10 之间
      

  2.   

    可是gnum的值是不一定的,我怎么实现呢?
      

  3.   

    既然这样,你又凭什么定义 gtime:array[1..10] 呢?
    可以用动态数组 gtime:array of Integer;
    然后:SetLength(gtime,StrToInt(Edit1.Text));
      

  4.   

    对,应该用动态数组,DELPHI 4 后引进的一个锐利武器。