为什么我的程序不输出我的所有数据?

时间:2013-03-22 00:05:08

标签: pascal output lazarus

program ZZX1;

{$mode objfpc}{$H+}

uses
  crt,
  wincrt,
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this  };
type
  Masquerader = record
    Name, CountyCode: string;
    Payment: real;
   end;
var
  Applicant: array[1..10] of Masquerader;
  DemList: array[1..10] of string;
  BerList: array[1..10] of string;
  EsqList: array[1..10] of string;
  x:integer;
  Y:integer;
  DemCounter:integer;
  BerCounter:integer;
  EsqCounter:integer;
  DemAmount:real;
  BerAmount:real;
  EsqAmount:real;

 procedure LoadData;
 begin
    clrscr;
X:=0;
DemCounter:=0;
BerCounter:=0;
EsqCounter:=0;
DemAmount:=0;
BerAmount:=0;
EsqAmount:=0;
repeat
X:= x+1;
repeat
write('Enter Your County Code DemM or BerM or EsqM: ');
readln(Applicant[x].CountyCode);
until (Applicant[x].CountyCode= 'DemM') or (Applicant[x].CountyCode= 'BerM') or (Applicant[x].CountyCode= 'EsqM');
If Applicant[x].CountyCode = 'DemM' then
  begin
    write('Enter Your Name: ');
    readln(Applicant[x].Name);
    write('Enter Your Total Payment: ');
    readln(Applicant[x].Payment);
    clrscr;
    DemCounter:= DemCounter + 1;
    DemAmount:= DemAmount + Applicant[x].Payment;
    DemList[DemCounter]:= Applicant[x].Name;
  end;
If Applicant[x].CountyCode = 'BerM' then
  begin
    write('Enter Your Name: ');
    readln(Applicant[x].Name);
    write('Enter Your Total Payment: ');
    readln(Applicant[x].Payment);
    clrscr;
    BerCounter:= BerCounter + 1;
    BerAmount:= BerAmount + Applicant[x].Payment;
    BerList[BerCounter]:= Applicant[x].Name;
  end;
If Applicant[x].CountyCode = 'EsqM' then
  begin
    write('Enter Your Name: ');
    readln(Applicant[x].Name);
    write('Enter Your Total Payment: ');
    readln(Applicant[x].Payment);
    clrscr;
    EsqCounter:= EsqCounter + 1;
    EsqAmount:= EsqAmount + Applicant[x].Payment;
    EsqList[EsqCounter]:= Applicant[x].Name;
  end;
until x=6 ;

 end;

Procedure PrintData;
  begin
    Y:= 0;
    for y := 1 to 6 do
    begin
      writeln('Name: ', Applicant[y].Name);
      writeln('CountyCode: ', Applicant[y].CountyCode);
      writeln('Payment: ', Applicant[y].Payment:0:2);
      writeln;
    end;
    For Y:= 1 to DemCounter do
      begin
        writeln(DemList[Y]);
        writeln(DemCounter,'',' persons are registered in Demerara');
        writeln;
        writeln('DemTotal:$ ', DemAmount:0:2);
      end;
    For Y:= 1 to BerCounter do
      begin
        writeln(BerList[Y]);
        writeln(BerCounter,'',' persons are registered in Berbice');
        writeln;
        writeln('BerTotal:$ ', BerAmount:0:2);
      end;
    For Y:= 1 to EsqCounter do
      begin
        writeln(EsqList[Y]);
        writeln(EsqCounter,'',' persons are registered in Essequibo');
        writeln;
        writeln('EsqTotal:$ ', EsqAmount:0:2);
      end;
 end;
Procedure quit;
  begin
    writeln('Press <Enter> To Quit');
    readln;
  end;

begin
  LoadData;
  PrintData;
  quit;
end.

该计划目前收集6个人,并通过他们的计数组对他们进行分组,计算每个县收集的人员和资金总额。 当我运行下面的程序时,我的预期输出在屏幕上几秒钟然后消失,只剩下一段预期的输出(结束部分)。请协助。

1 个答案:

答案 0 :(得分:2)

如果程序到达程序readln;中的quit语句时键盘缓冲区中有字符,readln将读取这些字符并继续向前而不是等待进一步输入仍在进行中。

要检查这一点,请尝试将字符变量作为参数添加到readln并将字符的ASCII值写出(或在调试器中检查其值)以查看该变量后是否有任何内容readln

(编辑)

进一步思考之后,我想知道代码是否像:

For Y:= 1 to EsqCounter do
  begin
    writeln(EsqList[Y]);
    writeln(EsqCounter,'',' persons are registered in Essequibo');
    writeln;
    writeln('EsqTotal:$ ', EsqAmount:0:2);
  end;

......其实应该是这样的:

For Y:= 1 to EsqCounter do
  begin
    writeln(EsqList[Y]);
  end;
writeln(EsqCounter,'',' persons are registered in Essequibo');
writeln;
writeln('EsqTotal:$ ', EsqAmount:0:2);

...因为否则EsqCounterEsqTotal的相同值将输出EsqCounter次,这似乎是不必要的。