pascal中的未知运行时错误

时间:2017-01-26 17:51:15

标签: pascal freepascal

我无法找到这个错误的来源,我不断加入我的学校编码。每次我在我的数组中输入一个值并且for循环运行它会遇到运行时错误,或者程序的其余部分根据第一个值的信息运行,并且不接受任何其他值。有人可以向我解释如何解决这个问题吗?

program Montserrat_Elections;

Var cndnme : array [1..4] of String;
    votes : array [1..4] of Integer;
    highest, cnt : Integer;
    winner : string;

begin

 highest:= 0;
 winner:= 'Dan';

 For cnt:= 1 to 4 do

     begin
          Writeln('Please enter the first name of the candidate and the number of votes');
          Read (cndnme[cnt], votes[cnt]);

          If votes[cnt] > highest then
             highest := votes[cnt];
             winner := cndnme[cnt];
     end;
 Writeln('The winner of this constituency is', winner, 'with', highest, 'votes')
end.

1 个答案:

答案 0 :(得分:1)

读取更改为 Readln

Readln (cndnme[cnt], votes[cnt]);

然后您需要将开始...结束; 添加到此行:

If votes[cnt] > highest then
      begin
         highest := votes[cnt];
         winner := cndnme[cnt];
      end;
  

我更新&测试你的代码:

program Montserrat_Elections;

Var cndnme : array [1..4] of String;
    votes : array [1..4] of Integer;
    highest, cnt : Integer;
    winner : string;

begin
 highest:= 0;
 winner:= 'Dan';

 For cnt:= 1 to 4 do

 begin
      Writeln('Please enter the first name of the candidate and the number of votes');
      readln(cndnme[cnt], votes[cnt]);

      If votes[cnt] > highest then
      begin
         highest := votes[cnt];
         winner := cndnme[cnt];
      end;

 end;
 Writeln('The winner of this constituency is ', winner, ' with ', highest, ' votes');
 readln;
end.

结果:

Please enter the first name of the candidate and the number of votes                                                                                                            
Me                                                                                                                                                                              
23                                                                                                                                                                              
Please enter the first name of the candidate and the number of votes                                                                                                            
You                                                                                                                                                                             
42                                                                                                                                                                              
Please enter the first name of the candidate and the number of votes                                                                                                            
Ainun                                                                                                                                                                           
18                                                                                                                                                                              
Please enter the first name of the candidate and the number of votes                                                                                                            
Jhon                                                                                                                                                                            
38                                                                                                                                                                              
The winner of this constituency is You with 42 votes