에디드박스에서 값을 입력받아 메모란에 나타내어
합계와 평균, count를 구하고자 합니다.
처음 계산버튼을 누르면 값이 정상적으로 나타나는데
다시 입력을 받고 추가를 하여 계산버튼을 누르면
건수와 합계, 평균값이 잘못 나옵니다.
아시는 분은 좀 리필주세요!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
memo1: TMemo;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
//procedure Edit1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var score: array[1..40] of integer;
var current: integer;
procedure TForm1.Button1Click(Sender: TObject);
begin
//if (current=1) then
//begin
//current:=0;
//memo1.Lines.Clear;
//end else
//begin
score[current] := strtoint(edit1.text);
memo1.lines.add(inttostr(score[current]));
current := current + 1;
edit1.text :='';
edit1.setfocus;
end;
//end;
procedure TForm1.FormCreate(Sender: TObject);
begin
current := 1;
end;
procedure tform1.button2click(sender: tobject);
var sum, lp : integer;
begin
current := current-1;
if (current=0) then
begin
showmessage('처리할 데이타가 없습니다.');
edit1.SetFocus;
//memo1.lines.add('처리할 데이타가 없습니다.');
end else
begin
sum := 0;
for lp:=1 to current do begin
sum:= sum+score[lp];
end;
memo1.lines.add('데이타 수는 '+inttostr(current)+'개');
memo1.lines.add('데이타의 합은 '+inttostr(sum)+'입니다.');
memo1.lines.add('평균점은 '+floattostr(sum/current)+'입니다.');
end;
//current := 1;
//memo1.lines.clear;
end;
//procedure tform1.Edit1Click(Sender: TObject);
//begin
//edit1.text := '';
//end;
procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button4Click(Sender: TObject);
var sum, current : integer;
begin
current:=0;
sum:=0;
memo1.lines.clear;
edit1.SetFocus;
end;
end.
procedure TForm1.Button4Click(Sender: TObject);
var sum, current : integer; ->삭제
begin
current:=0; ->current:=1;
sum:=0; ->삭제
memo1.lines.clear;
edit1.SetFocus;
end