Pascal程序没有显示最大值

时间:2014-05-16 02:44:10

标签: counter record pascal records highest

请帮助我,我的程序只是继续显示姓氏和数字这里是程序。 我真的没有看到这里有什么问题,但我希望你们可能会看到它,请真的需要一些帮助它杀死我的大脑

Var
Counter:Integer;
MaxValue:Integer;
NumofVotes:Array[1..4]of Integer;
ChristianName:Array[1..4]of String;
Surname:Array[1..4]of String;
WinnerFName:String;
WinnerSName:String;
WinnerParty:String;
CandidateParty: Array[1..4] of String;

begin
  FOR Counter:= 1 to 4 Do
  If Counter= Counter Then
  begin
Writeln ('Please enter Christian name of Candidate ', Counter, ':');
Readln (ChristianName[Counter]);
Writeln ;
Writeln ('Please enter Surname of Candidate ', Counter, ':');
Readln (Surname[Counter]);
Writeln ;
Writeln ('Please enter number of votes received by Candidate ', Counter, ':');
Readln (NumOfVotes[Counter]);
Writeln ;
Writeln ('Please enter party of Candidate ', Counter, ':');
Readln (CandidateParty[Counter]);

  end;

  IF Counter = 1 THEN
begin
MaxValue:= NumofVotes[Counter];
WinnerFName:= ChristianName[Counter];
WinnerSName:= Surname[Counter];
WinnerParty:= CandidateParty[Counter];
end

ELSE
IF (NumofVotes[Counter]>MaxValue) THEN
begin
WinnerFName:= ChristianName[Counter];
WinnerSName:= Surname[Counter];
MaxValue:= NumofVotes[Counter];
WinnerParty:= CandidateParty[Counter]
end;
Writeln ;
Writeln ('The winner of the elections for this constituency is:');
Writeln ('FirstName: ', WinnerFName, ' Surname: ', WinnerSName);
Writeln ('From the ', WinnerParty);
Writeln (WinnerFName, ' has won with ', MaxValue, ' votes');
Writeln ;
Writeln  ;
Writeln ('Press <Enter> to end');
Readln  ;

end.

1 个答案:

答案 0 :(得分:0)

代码似乎缺少一对begin ... end关键字。结果,for指令在第一个end关键字处结束,即在读取所有候选数据之后。然后Counter4IF Counter = 1中的比较失败 - 执行进行到ELSE。在那里,未初始化的MaxValue可能为零,因此第四个NumofVotes大于此值,最终导致第四个候选人被打印。

要使代码按预期执行,请在begin之后添加FOR ... DO,然后在end

之前将其与Writeln ; Writeln ('The winner ... is:');关闭