implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
 var
   a:array[1..10] of integer;
   i,j:integer;
   swap:integer;
begin
    edit1.Clear;
    a[1]:=56;
    a[2]:=23;
    a[3]:=5;
    a[4]:=76;
    a[5]:=16;
    a[6]:=96;
    a[7]:=51;
    a[8]:=43;
    a[9]:=18;
    a[10]:=38;
    i:=10 div 2;
    while(i>=1) do
    begin
        swap:=a[i];
        j:=i*2;
        while(j<=10) do
        begin
            if a[j]<a[j+1] then
                j:=j+1;
            if a[j]<swap then
                break;
            a[i]:=a[j];
            i:=j;
            j:=j*2;
        end;
        a[i]:=swap;
        i:=i-1;
    end;
    for i:=1 to 10 do
      edit1.Text:=edit1.Text+' '+inttostr(a[i]);  //当把这一行去掉之后就没事了,但不能显示筛选侯的数组了。
end;end.
编译可以通过,但是一运行的话就会出现错误。
我感觉好像就不能输出筛选后的数组,显示其中一个好像也不行。

解决方案 »

  1.   

    加一个变量
    var
     s:string;.....
    s:='';
    for i:=1 to 10 do 
      s:=s+inttostr(a[i]);
    edit1.text:=s;   
      

  2.   

    定义一个变量var
    s:string;
    .....
    s:='';
      for i:=1 to 10 do 
        s:=s+' '+inttostr(a[i]);
     edit1.text:=s; 
      

  3.   

    {这样声明}var 
       a: array of integer;
    {这样设长度} setlength(a,10);
    {这样取值} for i :=1 to High(a) do  
     begin
      ListBox1.Items.Add(inttostr(a[i]))
     end;
    以下的你就要检查一下:i:=10 div 2; 
        while(i>=1) do 
        begin 
            swap:=a[i]; 
            j:=i*2; 
            while(j <=10) do 
            begin 
                if a[j] <a[j+1] then 
                    j:=j+1; 
                if a[j] <swap then 
                    break; 
                a[i]:=a[j]; 
                i:=j; 
                j:=j*2; 
            end; 
            a[i]:=swap; 
            i:=i-1; 
        end; 
      

  4.   

    定义一个变量var
    s:string;
    .....
    s:='';
      for i:=1 to 10 do 
        s:=s+' '+inttostr(a[i]);
     edit1.text:=s; 
      

  5.   

    {这样声明}var 
       a: array of integer;
    {这样设长度} setlength(a,10);
    {这样取值} for i :=1 to High(a) do  
     begin
      ListBox1.Items.Add(inttostr(a[i]))
     end;
    以下的你就要检查一下:i:=10 div 2; 
        while(i>=1) do 
        begin 
            swap:=a[i]; 
            j:=i*2; 
            while(j <=10) do 
            begin 
                if a[j] <a[j+1] then 
                    j:=j+1; 
                if a[j] <swap then 
                    break; 
                a[i]:=a[j]; 
                i:=j; 
                j:=j*2; 
            end; 
            a[i]:=swap; 
            i:=i-1; 
        end; 
      

  6.   

    如果是排序试看以下代码是否适用:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure Sort(var A:array of integer);
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.Sort(var A:array of integer);
    var
      i,j,t:integer;
    begin
      for i:=low(A) to High(A)-1  do
      for j:=High(A) downto i+1 do
      if A[i]>A[j] then
      begin
        t:=A[i];A[i]:=A[j];A[j]:=t;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
       a: array of integer;
       i,j,l:integer;
       swap:integer;
    begin 
        setlength(a,10);
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;     Sort(a);   for l :=0 to 9 do
    begin
      ListBox1.Items.Add(inttostr(a[l]))
    end;
    a:=nil;end;
    end.
      

  7.   

    总发还倒贴,再重发一次:如果是排序试看以下代码是否适用:
    [code=Delphi(Pascal)]
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure Sort(var A:array of integer);
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.Sort(var A:array of integer);
    var
      i,j,t:integer;
    begin
      for i:=low(A) to High(A)-1  do
      for j:=High(A) downto i+1 do
      if A[i]>A[j] then
      begin
        t:=A[i];A[i]:=A[j];A[j]:=t;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
       a: array of integer;
       i,j,l:integer;
       swap:integer;
    begin 
        setlength(a,10);
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;     Sort(a);   for l :=0 to 9 do
    begin
      ListBox1.Items.Add(inttostr(a[l]))
    end;
    a:=nil;
    end;
    end.
    [/code]
      

  8.   

    总发还倒贴,再重发一次:如果是排序试看以下代码是否适用:
    [code=Delphi(Pascal)]
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure Sort(var A:array of integer);
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.Sort(var A:array of integer);
    var
      i,j,t:integer;
    begin
      for i:=low(A) to High(A)-1  do
      for j:=High(A) downto i+1 do
      if A[i]>A[j] then
      begin
        t:=A[i];A[i]:=A[j];A[j]:=t;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
       a: array of integer;
       i,j,l:integer;
       swap:integer;
    begin 
        setlength(a,10);
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;     Sort(a);   for l :=0 to 9 do
    begin
      ListBox1.Items.Add(inttostr(a[l]))
    end;
    a:=nil;
    end;
    end.
    [/code]
      

  9.   

    楼主的算法有问题
    i:=10 div 2; 
        while(i>=1) do 
        begin 
            swap:=a[i]; 
            j:=i*2;    //I为5
            while(j <=10) do 
            begin 
                if a[j] <a[j+1] then 
                    j:=j+1; //J 为11了,越界了
      

  10.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure lSort(var A:array of integer);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.lSort(var A:array of integer);
    var
      i,j,t:integer;
    begin
      for i:=low(A) to High(A)-1  do
      for j:=High(A) downto i+1 do
      if A[i]>A[j] then
      begin
        t:=A[i];A[i]:=A[j];A[j]:=t;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var 
       a:array of integer;
       i:integer;
       s1: TStringList;
    begin
     SetLength(a,10);{指定长度}
     edit1.Clear;
     s1:=TStringList.Create;
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;
     lSort(a);{-------------------排序}
     for i := 0 to 9 do
     s1.Add(inttostr(a[i]));
     edit1.Text:=s1.Strings[s1.Count-1]; {得到最大值}
     a:=nil;
    end;end.
      

  11.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure lSort(var A:array of integer);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.lSort(var A:array of integer);
    var
      i,j,t:integer;
    begin
      for i:=low(A) to High(A)-1  do
      for j:=High(A) downto i+1 do
      if A[i]>A[j] then
      begin
        t:=A[i];A[i]:=A[j];A[j]:=t;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var 
       a:array of integer;
       i:integer;
       s1: TStringList;
    begin
     SetLength(a,10);{指定长度}
     edit1.Clear;
     s1:=TStringList.Create;
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;
     lSort(a);{-------------------排序}
     for i := 0 to 9 do
     s1.Add(inttostr(a[i]));
     edit1.Text:=s1.Strings[s1.Count-1]; {得到最大值}
     a:=nil;
    end;end.
      

  12.   


    [code=Delphi(Pascal)]
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure lSort(var A:array of integer);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.lSort(var A:array of integer);
    var
      i,j,t:integer;
    begin
      for i:=low(A) to High(A)-1  do
      for j:=High(A) downto i+1 do
      if A[i]>A[j] then
      begin
        t:=A[i];A[i]:=A[j];A[j]:=t;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var 
       a:array of integer;
       i:integer;
       s1: TStringList;
    begin
     SetLength(a,10);{指定长度}
     edit1.Clear;
     s1:=TStringList.Create;
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;
     lSort(a);{-------------------排序}
     for i := 0 to 9 do
     s1.Add(inttostr(a[i]));
     edit1.Text:=s1.Strings[s1.Count-1]; {得到最大值}
     a:=nil;
    end;end.
    [/code]
      

  13.   


    [code=Delphi(Pascal)]
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure lSort(var A:array of integer);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.lSort(var A:array of integer);
    var
      i,j,t:integer;
    begin
      for i:=low(A) to High(A)-1  do
      for j:=High(A) downto i+1 do
      if A[i]>A[j] then
      begin
        t:=A[i];A[i]:=A[j];A[j]:=t;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var 
       a:array of integer;
       i:integer;
       s1: TStringList;
    begin
     SetLength(a,10);{指定长度}
     edit1.Clear;
     s1:=TStringList.Create;
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;
     lSort(a);{-------------------排序}
     for i := 0 to 9 do
     s1.Add(inttostr(a[i]));
     edit1.Text:=s1.Strings[s1.Count-1]; {得到最大值}
     a:=nil;
    end;end.
    [/code]
      

  14.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure lSort(var A:array of integer);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    procedure TForm1.lSort(var A:array of integer);
    var
      i,j,t:integer;
    begin
      for i:=low(A) to High(A)-1  do
      for j:=High(A) downto i+1 do
      if A[i]>A[j] then
      begin
        t:=A[i];A[i]:=A[j];A[j]:=t;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var 
       a:array of integer;
       i:integer;
       s1: TStringList;
    begin
     SetLength(a,10);{指定长度}
     edit1.Clear;
     s1:=TStringList.Create;
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;
     lSort(a);{-------------------排序}
     for i := 0 to 9 do
     s1.Add(inttostr(a[i]));
     edit1.Text:=s1.Strings[s1.Count-1]; {得到最大值}
     a:=nil;
    end;end.
      

  15.   

    也可以这样:procedure TForm1.Button1Click(Sender: TObject);
    var 
       a:array of integer;
       i:integer;
    begin
     SetLength(a,10);{指定长度}
     edit1.Clear;
     s1:=TStringList.Create;
        a[0]:=56;
        a[1]:=23;
        a[2]:=5;
        a[3]:=76;
        a[4]:=16;
        a[5]:=96;
        a[6]:=51;
        a[7]:=43;
        a[8]:=18;
        a[9]:=38;
     lSort(a);
     edit1.Text:=inttostr(a[9]);{得到最大值}
     a:=nil;
    end;