任何人都可以更正此 Pascal 代码
Anyone can correct this Pascal code
我需要此代码的帮助。我不知道帕斯卡。但我必须用 Pascal 编写代码。我试过。但是有一些 errors.Can 有人帮助我吗?
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.
错误是:
source.pas(14,3) 错误:非法表达式
source.pas(16,5) 错误:非法表达式
source.pas(16,5) 致命:语法错误,“;”预期但 "FOR" 找到
如果您学会正确缩进(格式化)您的代码,问题就很清楚了。此答案基于您 post 编写的原始代码,在您添加更多内容并删除一些内容以进行更改之前。但是,答案中的信息仍然与问题相关。
您post编辑的内容:
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 不支持预递增或 post-递增运算符,因此 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.
有关 Pascal 中 begin..end
的语法和使用的更多信息,请参阅我为
写的这个答案
我需要此代码的帮助。我不知道帕斯卡。但我必须用 Pascal 编写代码。我试过。但是有一些 errors.Can 有人帮助我吗?
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.
错误是:
source.pas(14,3) 错误:非法表达式
source.pas(16,5) 错误:非法表达式
source.pas(16,5) 致命:语法错误,“;”预期但 "FOR" 找到
如果您学会正确缩进(格式化)您的代码,问题就很清楚了。此答案基于您 post 编写的原始代码,在您添加更多内容并删除一些内容以进行更改之前。但是,答案中的信息仍然与问题相关。
您post编辑的内容:
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 不支持预递增或 post-递增运算符,因此 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.
有关 Pascal 中 begin..end
的语法和使用的更多信息,请参阅我为