2017-04-19 71 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

相關問題