2017-04-19 74 views
-3

我需要帮助的代码。我不知道帕斯卡。但我必须用pascal写一个代码。我试过了。但是有一些错误。有人能帮助我吗?任何人都可以更正此Pascal代码

Program Arrayexamp(output); 

    Var 
    counter,index,input: Integer; 
    A: Array[1..15] of Integer; 
    B: Array[1..15] of Integer; 

begin 
    For index := 1 to 15 do 
    begin 
    read(input); 
    A[index] := input; 
    index++ 
    end 

    For counter := 1 to 15 do 
    begin 
    if (counter mod 2 = 0) Then 
     B[counter] = A[counter] * 3 
    Else B[counter]=A[counter]-5; 
    end 
end. 

的errores是:

source.pas(14,3)错误:非法表达

source.pas(16,5)错误:非法表达

source.pas (16,5)致命:语法错误,“;”预计但“FOR”发现

+4

*存在一些误区*是无用的,除非你告诉我们这些错误究竟是什么。他们在屏幕上,就在你的面前。没有理由不把它们包含在你的文章中,所以我们也有它们。此外,如果您正确格式化代码,问题就非常明确。 –

+0

可能的重复[适当的结构语法为帕斯卡尔如果然后开始结束和; (在Inno Setup中)](http://stackoverflow.com/questions/28221394/proper-structure-syntax-for-pascal-if-then-begin-end-and-in-inno-setup) –

+0

@nil:No , 不是。还有其他问题。阅读我的答案。 (我写了你的建议副本的答案,并将其链接到我的帖子中,这不是重复的。) –

回答

3

如果您学会正确缩进(格式化)您的代码,问题就非常清楚。此答案基于您发布的原始代码,然后添加更多内容并删除一些内容以进行更改。然而,答案中的信息仍然与问题有关。

你贴什么:

Program Arrayexamp(output); Var counter,index,input: Integer; A : Array[1..15] of Integer; 

begin 

For index := 1 to 15 do 
begin read(input); 
A[index] := input; 
    index++ 
end; 

begin 
For index := 1 to 15 do 

If (counter mod 2 = 0) Then B[counter]=A[counter]*3 Else B[counter]=A[counter]-5; end. 

如何它看起来正确格式化:

Program Arrayexamp(output); 

Var 
    counter,index,input: Integer; 
    A : Array[1..15] of Integer; 

begin 
    For index := 1 to 15 do 
    begin 
    read(input); 
    A[index] := input; 
    index++ 
    end; 

    begin 
    For index := 1 to 15 do 
     If (counter mod 2 = 0) Then 
     B[counter] = A[counter] * 3 
     Else B[counter]=A[counter]-5; 
end. 

的问题是清楚的:你有没有begin在下部区段匹配的end;。实际上,begin是完全不必要的,可以删除。

第二个问题是for循环本身会增加循环变量,因此在循环内修改该计数器是非法的。删除index++;行。 (请参阅下一段。)

第三个问题是Pascal不支持预增或后增操作符,因此index++是无效的语法。改为使用index := index + 1;Inc(index);

写了正确的代码:

Program Arrayexamp(output); 

Var 
    counter,index,input: Integer; 
    A: Array[1..15] of Integer; 

begin 
    For index := 1 to 15 do 
    begin 
    read(input); 
    A[index] := input; 
    end; 

    For index := 1 to 15 do 
    if (counter mod 2 = 0) Then 
     B[counter] = A[counter] * 3 
    Else B[counter]=A[counter] - 5; 
end. 

有关语法和使用的begin..end帕斯卡的更多信息,请参考这个答案我写了Proper structure syntax for Pascal if then begin end

相关问题