2017-01-26 65 views
0

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

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

阅读关于单独的语句和复合语句。你需要围绕你的身体开始/结束。 –

+1

你不能在一个READ命令中读取一个字符串和一个整数,因为从一个整数中分隔字符串的分隔符是什么?读取空间也是允许​​的(something_string) –

+0

@David:问题是'Read'语句;它与'if'无关。 –

回答

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 
相关问题