数组赋值问题,不知为何会这样(100)

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

SP229

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个数组:const ss: array[0..11] of string=('A','B','C','D','E','F','G','H','I','J','K','L');想给一个动态数组赋值,在一个EDIT中输入比如“F”,求出F在数组SS中的位置,那么这个动态数组AA就从F开始,['F','G','H','I','J','K','L','A','B','C','D','E']输入“G”那么就从G开始:AA=['G','H','I','J','K','L','A','B','C','D','E','F],现在我写了一段代码,有些可以 ,有些不行,请大家帮我看看。procedure TForm1.btn3Click(Sender: TObject);const ss: array[0..11] of string=('A','B','C','D','E','F','G','H','I','J','K','L');var i,J: Integer; n: Integer;begin mmo1.Clear; n:=AnsiIndexText(Edit1.Text,ss); Edit3.Text :=IntToStr(n); SetLength(AA,High(SS)); for i:=0 to 11 do begin if i<n then J:=I+(High(SS)-N) else J:=I-N; AA:=SS[J]; mmo1.Lines.Add(SS[J]) end;end;
 
procedure TForm1.Button1Click(Sender: TObject);const ss: array[0..11] of string=('A','B','C','D','E','F','G','H','I','J','K','L');var i, j: integer; aa: array of string;begin j := -1; for i := 0 to length(ss) - 1 do begin if edit1.Text = ss then begin j := i; break; end; end; if j <> -1 then begin setlength(aa, length(ss)); for i := 0 to length(aa) - 1 do begin // aa := ss[(j + length(ss)) mod (length(ss))]; //又看了下,不加也没事 aa := ss[j mod (length(ss))]; inc(j); end; end; Edit2.Text :=''; for i := 0 to length(aa) - 1 do begin Edit2.Text := Edit2.Text + aa; end;end;
 
试试下面的代码:procedure TForm1.btnCalcClick(Sender: TObject);const SS: array[0..11] of string = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L');var I, N: Integer; Tmp: array[0..11] of string; // 存放结果begin mmo1.Clear; N := AnsiIndexText(edt1.Text, SS); for I := 0 to Length(SS) - N - 1 do Tmp := SS[I + N]; for I := 0 to N - 1 do Tmp[Length(SS) - N + I] := SS;end;
 
还可以简化一下,如下:procedure TForm1.btnCalcClick(Sender: TObject);const SS: array[0..11] of string = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L');var I, N: Integer; Tmp: array of string;begin mmo1.Clear; SetLength(Tmp, Length(SS)); N := AnsiIndexText(edt1.Text, SS); for I := 0 to Length(SS) - 1 do if I < Length(SS) - N then Tmp := SS[I + N] else Tmp := SS[I + N - Length(SS)];end;
 
谢谢两位,两个的代码都可以。
 

Similar threads

后退
顶部