0

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

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.
4

1 回答 1

0

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

要使代码达到您的预期,请begin在之后添加并在之前FOR ... DO关闭它endWriteln ; Writeln ('The winner ... is:');

于 2014-05-19T22:36:11.447 回答