델파이코딩_개발일지

델파이코딩_while do 구문 사용해서 등차수열 합 구하기

천재메기 2023. 2. 8. 18:37

구조를 설명하겠다.

먼저 A_input 이라는 TCxCurrencyEdit 이라는 컴포넌트에서 입력값을 받고 

B_input 라는 이름으로 마찬가지로 동일한 컴포넌트에서 입력값을 받는다.

 

TCxCurrencyEdit : 값을 입력 받는 곳

TMemo : 계산한 값을 나타내는 메모장

TButton : 확인 버튼

TLabel : 메뉴 이름을 나타내 주는 이름표

 

이러한 컴포넌트들을 활용하여 화면을 구성할 것이다.

 

알고리즘

1. 커런시에딧에서 A,B 라는 숫자를 입력받는다.

2. 확인 버튼을 클릭하면 값을 전달한다.

3. 메모장에서 계산 값을 나타내준다.

 

위와 같은 순서로 논리가 돌아가는 것!

 

그러면 코드를 보자.

 

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, cxControls, cxContainer, cxEdit, cxTextEdit, cxCurrencyEdit;

type
  TForm3 = class(TForm)
    A_input: TcxCurrencyEdit;
    btnOK: TButton;
    A_label: TLabel;
    B_label: TLabel;
    B_input: TcxCurrencyEdit;
    mm_result: TMemo;
    procedure mm_resultChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation
var
  A, B, i, j, sum : integer ;
{$R *.dfm}

procedure TForm3.mm_resultChange(Sender: TObject);

begin
  A := A_input.EditValue ;
  B := B_input.EditValue ;

  sum := 0;
  i := A;
  j := B;

  while i < B+1 do
  begin
  sum := sum + i;
  i := i + 1;

  end;

  mm_result.lines.Add(inttostr(sum));



end;

end.

whlile 로 짜려니까 한참 헤맸다.

오픈 튜토리얼스에서 설명한 아래 내용을 보고나서야 간신히 해결했다.

조건식을 변경하는 코드가 명령문에 없거나 잘못구현되면 무한반복 된다는 것이다. 

 

난 처음에 그냥 이렇게 짰다가 무한 반복이 됐었다. ;;;; 

델파이가 거의 멈추는 느낌이었다...

while A <B do
begin
  sum := sum + A;
end;