Loops 如何修复此错误?帕斯卡
该计划的目标是在10次尝试中找到一个随机数。我设法用免费的PascalIDE编译了它,问题是程序直到找到数字才停止。但是,当我尝试使用联机编译时,会出现以下错误: prog.pas:在主程序中: 程序pas:26:错误:“转到”到无效目标Loops 如何修复此错误?帕斯卡,loops,compiler-errors,pascal,goto,Loops,Compiler Errors,Pascal,Goto,该计划的目标是在10次尝试中找到一个随机数。我设法用免费的PascalIDE编译了它,问题是程序直到找到数字才停止。但是,当我尝试使用联机编译时,会出现以下错误: prog.pas:在主程序中: 程序pas:26:错误:“转到”到无效目标 是因为标签/goto不能在循环中立即跳转吗?下面给出的goto语句后缺少分号: PROGRAM RandomNumber; Var rand,count,guess : integer; LABEL correct, loop, wrong, end1; be
是因为标签/goto不能在循环中立即跳转吗?下面给出的goto语句后缺少分号:
PROGRAM RandomNumber;
Var rand,count,guess : integer;
LABEL correct, loop, wrong, end1;
begin
{Initialization, so that random numbers are drawn}
Randomize;
count :=0;
repeat
loop:
count := count+1;
{Random(i) Creates random numbers between 0 and i.}
rand := Random(10);
guess := Random(10);
if rand=guess
then
goto correct
else
goto wrong;
until count > 10;
goto end1;
correct :
WriteLn('Correct');
goto end1;
wrong :
WriteLn('False Guess');
goto loop;
end1 :
WriteLn;
end.
下面给出的goto语句后缺少分号:
PROGRAM RandomNumber;
Var rand,count,guess : integer;
LABEL correct, loop, wrong, end1;
begin
{Initialization, so that random numbers are drawn}
Randomize;
count :=0;
repeat
loop:
count := count+1;
{Random(i) Creates random numbers between 0 and i.}
rand := Random(10);
guess := Random(10);
if rand=guess
then
goto correct
else
goto wrong;
until count > 10;
goto end1;
correct :
WriteLn('Correct');
goto end1;
wrong :
WriteLn('False Guess');
goto loop;
end1 :
WriteLn;
end.
下面给出的goto语句后缺少分号:
PROGRAM RandomNumber;
Var rand,count,guess : integer;
LABEL correct, loop, wrong, end1;
begin
{Initialization, so that random numbers are drawn}
Randomize;
count :=0;
repeat
loop:
count := count+1;
{Random(i) Creates random numbers between 0 and i.}
rand := Random(10);
guess := Random(10);
if rand=guess
then
goto correct
else
goto wrong;
until count > 10;
goto end1;
correct :
WriteLn('Correct');
goto end1;
wrong :
WriteLn('False Guess');
goto loop;
end1 :
WriteLn;
end.
下面给出的goto语句后缺少分号:
PROGRAM RandomNumber;
Var rand,count,guess : integer;
LABEL correct, loop, wrong, end1;
begin
{Initialization, so that random numbers are drawn}
Randomize;
count :=0;
repeat
loop:
count := count+1;
{Random(i) Creates random numbers between 0 and i.}
rand := Random(10);
guess := Random(10);
if rand=guess
then
goto correct
else
goto wrong;
until count > 10;
goto end1;
correct :
WriteLn('Correct');
goto end1;
wrong :
WriteLn('False Guess');
goto loop;
end1 :
WriteLn;
end.
近似的“为什么它不工作?!”答案是:缺少分号
别难过。这很容易错过。Pascal允许在条件结构或循环结构之后插入一行代码,或者将开始
/结束
序列作为虚拟语句。这种选择与Pascal使用分号作为语句分隔符而不是终止符的做法有着不好的相互作用(这对解析器设计者来说是一个很好的区别,但多年来已经证明这一点非常微妙且容易出错,特别是对于那些不熟悉该语言的人来说。请注意缺少的分号和多余的分号!
有两件事可以帮助你:总是,总是,总是使用强有力的、一致的、有条理的缩进来帮助你展示代码的逻辑结构。你可能会发现,在语句或多语句块可能适合的任何上下文中,总是使用开始
/结束
块更容易(即使块包含一行)。起初这可能看起来很愚蠢,但当您开始向程序中添加语句(包括临时调试打印语句)时,使用在所有情况下都有效的单个构造确实有帮助
程序失败的不太直接的原因不是分号。这是因为您使用了一种完全非结构化的方法来控制程序中的流。您在GOTO
中到处乱跑,即使您不需要,即使有更简单、更干净、更结构化的替代方法。例如,c考虑者:
goto correct
else
goto wrong
编译并运行几次后,它看起来像:
PROGRAM RandomNumber;
Var rand, count, guess, MaxGuesses : integer;
begin
{Initialization, so that random numbers are drawn}
Randomize;
MaxGuesses := 10;
count := 0;
repeat
count := count + 1;
{Random(i) Creates random numbers between 0 and i.}
rand := Random(10);
guess := Random(10);
if rand = guess
then begin
WriteLn('Correct');
break
end
else
WriteLn('False Guess');
until count >= MaxGuesses;
WriteLn;
if (rand = guess)
then
WriteLn('We had a winner (', guess, ') in ', count, ' tries.')
else
WriteLn('Sorry! No winner in ', MaxGuesses, ' tries.');
WriteLn;
end.
Pascal是20世纪60年代和70年代编程革命的重要组成部分,这场革命使“结构化编程”得到了广泛的应用。当人们迫切需要更好的结构时,不要使用较低级别的goto
-装载代码。最接近的“为什么它不工作?!”答案是:缺少分号
别难过。这很容易错过。Pascal允许在条件结构或循环结构之后出现一行代码,或者将开始
/结束
序列作为虚拟语句。这种选择与Pascal使用分号作为语句分隔符而不是终止符的做法有着严重的相互影响(对于解析器设计者来说,这是一个很好的区别,但多年来已经证明这是一个微妙且容易出错的区别,特别是对于那些不熟悉该语言的人。请注意缺少的分号和多余的分号!
有两件事可以帮助你:总是,总是,总是使用强有力的、一致的、有条理的缩进来帮助你展示代码的逻辑结构。你可能会发现,在语句或多语句块可能适合的任何上下文中,总是使用开始
/结束
块更容易(即使块包含一行)。起初这可能看起来很愚蠢,但当您开始向程序中添加语句(包括临时调试打印语句)时,使用在所有情况下都有效的单个构造确实有帮助
程序失败的不太直接的原因不是分号。这是因为您使用了一种完全非结构化的方法来控制程序中的流。您在GOTO
中到处乱跑,即使您不需要,即使有更简单、更干净、更结构化的替代方法。例如,c考虑者:
goto correct
else
goto wrong
编译并运行几次后,它看起来像:
PROGRAM RandomNumber;
Var rand, count, guess, MaxGuesses : integer;
begin
{Initialization, so that random numbers are drawn}
Randomize;
MaxGuesses := 10;
count := 0;
repeat
count := count + 1;
{Random(i) Creates random numbers between 0 and i.}
rand := Random(10);
guess := Random(10);
if rand = guess
then begin
WriteLn('Correct');
break
end
else
WriteLn('False Guess');
until count >= MaxGuesses;
WriteLn;
if (rand = guess)
then
WriteLn('We had a winner (', guess, ') in ', count, ' tries.')
else
WriteLn('Sorry! No winner in ', MaxGuesses, ' tries.');
WriteLn;
end.
Pascal是20世纪60年代和70年代编程革命的重要组成部分,这场革命使“结构化编程”得到了广泛的应用。当人们迫切需要更好的结构时,不要使用较低级别的goto
-装载代码。最接近的“为什么它不工作?!”答案是:缺少分号
别难过。这很容易错过。Pascal允许在条件结构或循环结构之后出现一行代码,或者将开始
/结束
序列作为虚拟语句。这种选择与Pascal使用分号作为语句分隔符而不是终止符的做法有着严重的相互影响(对于解析器设计者来说,这是一个很好的区别,但多年来已经证明这是一个微妙且容易出错的区别,特别是对于那些不熟悉该语言的人。请注意缺少的分号和多余的分号!
有两件事可以帮助你:总是,总是,总是使用强有力的、一致的、有条理的缩进来帮助你展示代码的逻辑结构。你可能会发现,在语句或多语句块可能适合的任何上下文中,总是使用开始
/结束
块更容易(即使块包含一行)。起初这可能看起来很愚蠢,但当您开始向程序中添加语句(包括临时调试打印语句)时,使用在所有情况下都有效的单个构造确实有帮助
程序失败的不太直接的原因不是分号。这是因为您使用了一种完全非结构化的方法来控制程序中的流。您在GOTO
中到处乱跑,即使您不需要,即使有更简单、更干净、更结构化的替代方法。例如,c考虑者:
goto correct
else
goto wrong
编撰