Pascal 帕斯卡比较误差
我的简单Pascal代码有问题。(我刚开始学帕斯卡语。) 这是一个关于年龄比较的代码,其余的可以通过代码看到Pascal 帕斯卡比较误差,pascal,Pascal,我的简单Pascal代码有问题。(我刚开始学帕斯卡语。) 这是一个关于年龄比较的代码,其余的可以通过代码看到 program Test; uses crt; var age : real; Begin writeln('Enter your age: '); readln(age); if age>18 then if age<100 then Begin clrscr; textcolor(lightgreen); writeln('Access Granted'); end
program Test;
uses crt;
var
age : real;
Begin
writeln('Enter your age: ');
readln(age);
if age>18 then
if age<100 then
Begin
clrscr;
textcolor(lightgreen);
writeln('Access Granted');
end
else
if age<18 then
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to young');
end
else
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to old');
end;
readln;
end.
但我没有得到任何输出。为什么?有时文本缩进可以帮助您了解问题所在。以下是添加缩进的代码:
program Test;
uses crt;
var
age : real;
Begin
writeln('Enter your age: ');
readln(age);
if age>18 then
if age<100 then
Begin
clrscr;
textcolor(lightgreen);
writeln('Access Granted');
end
else
if age<18 then
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to young');
end
else
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to old');
end;
readln;
end.
现在很容易看出,标记为您太年轻了的分支从未到达。它应该在age
小于18时执行,但if
语句嵌套到另一个if
语句中,该语句仅在age
大于18时调用它。因此,age
应该首先限定为大于18,然后小于18,以便执行该分支–现在您可以看到为什么没有得到预期结果
预期逻辑可能通过以下方式实现:
if age>18 then
if age<100 then
... // Access Granted
else // i.e. "if age >= 100"
... // You are way too old
else // now this "else" belongs to the first "if"
... // You are way too young
;
如果年龄>18岁,则
如果18岁
到年龄>=18
,则18岁不符合“太年轻”的条件。有时文本缩进可以帮助您查看问题。以下是添加缩进的代码:
program Test;
uses crt;
var
age : real;
Begin
writeln('Enter your age: ');
readln(age);
if age>18 then
if age<100 then
Begin
clrscr;
textcolor(lightgreen);
writeln('Access Granted');
end
else
if age<18 then
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to young');
end
else
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to old');
end;
readln;
end.
现在很容易看出,标记为您太年轻了的分支从未到达。它应该在age
小于18时执行,但if
语句嵌套到另一个if
语句中,该语句仅在age
大于18时调用它。因此,age
应该首先限定为大于18,然后小于18,以便执行该分支–现在您可以看到为什么没有得到预期结果
预期逻辑可能通过以下方式实现:
if age>18 then
if age<100 then
... // Access Granted
else // i.e. "if age >= 100"
... // You are way too old
else // now this "else" belongs to the first "if"
... // You are way too young
;
如果年龄>18岁,则
如果18岁
到年龄>=18
,则18岁不符合“太年轻”的条件。有时文本缩进可以帮助您查看问题。以下是添加缩进的代码:
program Test;
uses crt;
var
age : real;
Begin
writeln('Enter your age: ');
readln(age);
if age>18 then
if age<100 then
Begin
clrscr;
textcolor(lightgreen);
writeln('Access Granted');
end
else
if age<18 then
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to young');
end
else
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to old');
end;
readln;
end.
现在很容易看出,标记为您太年轻了的分支从未到达。它应该在age
小于18时执行,但if
语句嵌套到另一个if
语句中,该语句仅在age
大于18时调用它。因此,age
应该首先限定为大于18,然后小于18,以便执行该分支–现在您可以看到为什么没有得到预期结果
预期逻辑可能通过以下方式实现:
if age>18 then
if age<100 then
... // Access Granted
else // i.e. "if age >= 100"
... // You are way too old
else // now this "else" belongs to the first "if"
... // You are way too young
;
如果年龄>18岁,则
如果18岁
到年龄>=18
,则18岁不符合“太年轻”的条件。有时文本缩进可以帮助您查看问题。以下是添加缩进的代码:
program Test;
uses crt;
var
age : real;
Begin
writeln('Enter your age: ');
readln(age);
if age>18 then
if age<100 then
Begin
clrscr;
textcolor(lightgreen);
writeln('Access Granted');
end
else
if age<18 then
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to young');
end
else
Begin
clrscr;
textcolor(lightred);
writeln('ACCESS DENIED');
writeln('Reason:You are way to old');
end;
readln;
end.
现在很容易看出,标记为您太年轻了的分支从未到达。它应该在age
小于18时执行,但if
语句嵌套到另一个if
语句中,该语句仅在age
大于18时调用它。因此,age
应该首先限定为大于18,然后小于18,以便执行该分支–现在您可以看到为什么没有得到预期结果
预期逻辑可能通过以下方式实现:
if age>18 then
if age<100 then
... // Access Granted
else // i.e. "if age >= 100"
... // You are way too old
else // now this "else" belongs to the first "if"
... // You are way too young
;
如果年龄>18岁,则
如果18岁
到年龄>=18
,那么18岁就不符合“太年轻”的条件。有什么问题?你得到了什么结果,你期望什么?是的,对不起,我忘了提那个。当你输入18岁以下的年龄时,文本不会出现。我希望它会显示“writeln(‘访问被拒绝’);writeln(‘原因:你太年轻了’);”如果年龄大于18岁,你试过写吗“if age>18和age一些缩进,包括排列您的if
/else
/结束
关键字,以便您可以随时查看程序的结构,这可能会有所帮助…”。。。特别是,如果你的年龄是18岁,那么这似乎永远不会是真的……问题是什么?你得到了什么结果,你期望什么?是的,对不起,我忘了提那个。当你输入18岁以下的年龄时,文本不会出现。我希望它会显示“writeln(‘访问被拒绝’);writeln(‘原因:你太年轻了’);”如果年龄大于18岁,你试过写吗“if age>18和age一些缩进,包括排列您的if
/else
/结束
关键字,以便您可以随时查看程序的结构,这可能会有所帮助…”。。。特别是,如果你的年龄是18岁,那么这似乎永远不会是真的……问题是什么?你得到了什么结果,你期望什么?是的,对不起,我忘了提那个。当你输入18岁以下的年龄时,文本不会出现。我希望它会显示“writeln(‘访问被拒绝’);writeln(‘原因:你太年轻了’);”如果年龄大于18岁,你试过写吗“if age>18和age一些缩进,包括排列您的if
/else
/结束
关键字,以便您可以随时查看程序的结构,这可能会有所帮助…”。。。特别是,如果你的年龄是18岁,那么这似乎永远不会是真的……问题是什么?你得到了什么结果,你期望什么?是的,对不起,我忘了提那个。当你输入18岁以下的年龄时,文本不会出现。我希望它会显示“writeln(‘访问被拒绝’);writeln(‘原因:你太年轻了’);”如果年龄大于18岁,你试过写吗“if age>18和age一些缩进,包括排列您的if
/else
/结束
关键字,以便您可以随时查看程序的结构,这可能会有所帮助…”。。。特别是,似乎你已经18岁了,所以这永远不会是真的。。。