2017-10-08 5 views
-2

この問題は、数値が大きい場合にのみ表示され、大きい場合には12が表示されます。Forループは目標到達後も継続します。 Delphi

これらの2枚の写真を一度にキャプチャしました。どのように可能ですか?

Forループは0から12-1 = 11にする必要がありますか?

しかし、代わりにwhileループを使用すると、正常に動作します。

それは私のせいかデルファイですか?

P.S.下にコードダウン。 1 1

unit Unit1; 
interface 

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

type 
    TForm1 = class(TForm) 
    StringGrid1: TStringGrid; 
    Button1: TButton; 
    Edit1: TEdit; 
    Button2: TButton; 
    Label1: TLabel; 
    Button3: TButton; 
    Label2: TLabel; 
    Label3: TLabel; 
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
    procedure StringGrid1KeyPress(Sender: TObject; var Key: Char); 
    procedure Button3Click(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 
    n:Integer; 

implementation 

{$R *.dfm} 

procedure TForm1.Button1Click(Sender: TObject); //Button, that sets array length 
var 
    i, index:Integer; 
begin 
    val(Edit1.Text, n, index);     
    if(index<>0) then 
    begin 
     ShowMessage('Wrong number'); 
     Edit1.Clear(); 
     exit; 
    end; 
    StringGrid1.ColCount:=n; 
    for i:=0 to n-1 do       
     StringGrid1.Cells[i,0]:=IntToStr(i+1); 
    StringGrid1.SetFocus(); 
end; 

procedure TForm1.Button2Click(Sender: TObject);  //Main button 
var 
    i, index:Integer; 
    a:array[0..10] of Real; 
    denom, sum:Real; 
begin 
    i:=0; 
    sum:=0; 
    denom:=-1; 

//that for loop from screenshot is here 

    for i:=0 to n-1 do 
    //while i<=(n-1) do 
    begin 
    Val(StringGrid1.cells[i,1], a[i], index);  
    if(index<>0) then 
    begin 
     ShowMessage('Wrong number with ' + IntToStr(i+1) + ' Id'); 
     StringGrid1.Col:=i; 
     StringGrid1.Row:=1; 
     StringGrid1.SetFocus(); 
     exit; 
    end; 
    a[i]:=a[i]/denom;        
    sum:=sum+a[i]; 
    StringGrid1.Cells[i,2]:=FloatToStrF(a[i],ffFixed,5,3);  
    denom:=-denom*(i+2); 
    //Inc(i); 
    end; 
    Label2.Caption:=FloatToStrF(sum,ffFixed,5,3); 
end; 

//code down bellow just allow to go to another cell by pressing Enter 


procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: Char); 
begin 
    if (Key=#13) and (StringGrid1.Col=(n-1)) then 
     Button2.SetFocus() 
    else if (Key=#13) and (StringGrid1.Col<>(n-1)) then 
     StringGrid1.Col:=StringGrid1.Col+1; 
end; 

procedure TForm1.Button3Click(Sender: TObject); 
begin 
    Close(); 
end; 

end. 
+3

[ここをクリック](https://stackoverflow.com/a/35478474/2292722)を参照してください。 –

+0

exit文が入ったループですか?私はあなたのコードを読んで、彼らの誤った論理を知る必要はない。条件付きループを停止する必要がある場合は、条件付きループ(until untilまたはdo while)を使用し、無条件ループは使用しないでください。ループが終了した後のすべてのコードは、出口が起動されたときには実行されません。これは非常に読めないコードです – GuidoG

答えて

-2

アレイaサイズ は、次いで、細胞の量が少なかったです。

+1

あなたは正しいですが、なぜこれが重要なのか説明しません。 – Dsm

+0

あなたは@Dsmのように答えに説明を追加するか、削除してください。 – MartynA

-1

「これさえ可能にする方法である」があなたの質問に答えるためにとして...

あなたの画面では、nは12としては、Kermationによって指摘され、最高の指数は10であるので、私はあるとき、 11の場合、範囲チェックが有効になっていない限り、[11](i = 11)に書き込むと、他のものを上書きします。これはローカル変数領域にあります。たとえば、ループの開始時に計算されるforループに使用される制限のように見えない内部変数やiのようなものでもあります。これが許されると、何かが可能になります。

もちろん、問題の正確な表現はコンパイラのあるバージョンから別のバージョンへと非常に変わります。 1つのバージョンでは、それを取り除く可能性があります。別のあなたはしません。さまざまな状況で*ループ制御変数*がどのように使用されるかの説明については、

関連する問題