哪一部分是错的?

时间:2016-01-25 21:52:16

标签: pascal freepascal

我无法弄清楚这段代码的哪一部分不正确:

Program TrinbarjamWinningParty;
VAR
  DAP:string;
  WNA:char;
  UPM:char;
  PDR:char;
  num1:integer;
  num2:integer;
  num3:integer;
  num4:integer;
  count:integer;
  partyAcronym:array[1..4] of string;
  votesReceived:array[1..4] of integer;
  winnerVotes:integer;
  winningParty:string;

Begin
  partyAcronym[1]:='DAP';
  partyAcronym[2]:='WNA';
  partyAcronym[3]:='UPM';
  partyAcronym[4]:='PDR';
  winnerVotes:=0;

  Writeln('                   Trinbarjam Winning Party');
  for Count := 1 to 4 do
    Begin
      Writeln('Enter Votes Received for: ',partyAcronym[count]);
      Readln(votesReceived[count]);
    end;

  Begin
    partyAcronym := winningParty;
    votesReceived := winnerVotes;
    for count:=1 to 4 do
       Begin
         if count > winnerVotes then
           winnervotes := count
       end;

    writeln('Winning Pary:', winningParty);
    writeln('Votes Received:', winnerVotes);
    readln();
End.

1 个答案:

答案 0 :(得分:1)

首先:

Begin partyAcronym := winningParty;

该怎么办? 此begin没有相应的end,可能应该删除。此时winningParty仍然未初始化。另外partyAcronym是一个数组,你不能给它分配一个字符串。

对于其他人:我想我可以在各行之间阅读你想要做的事情并自由地相应地调整你的代码。

请注意,您仍然需要处理多次平等投票的情况。

program TrinbarjamWinningParty;
var
  index:integer;
  partyAcronym:array[1..4] of string;
  votesReceived:array[1..4] of integer;
  winningPartyIndex:integer;

begin
  partyAcronym[1] := 'DAP';
  partyAcronym[2] := 'WNA';
  partyAcronym[3] := 'UPM';
  partyAcronym[4] := 'PDR';

  winningPartyIndex := 1;

  Writeln('Trinbarjam Winning Party');

  for index := 1 to 4 do
  begin
    Writeln('Enter Votes Received for: ', partyAcronym[index]);
    Readln(votesReceived[index]);
    if votesReceived[index] > votesReceived[winningPartyIndex] then
      winningPartyIndex := index;
  end;

  writeln('Winning Party:', partyAcronym[winningPartyIndex]);
  writeln('Votes Received:', votesReceived[winningPartyIndex]);

  readln();
end.