帕斯卡比较错误 [英] Pascal comparison error

查看:46
本文介绍了帕斯卡比较错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的非常简单的 Pascal 代码有问题.(我刚开始学习 Pascal.)

I have a problem with my very simple Pascal code. (I just started to learn Pascal.)

所以这是一个年龄比较代码,然后通过代码可以看到休息.

So it's about an age comparison code then rest can be seen through the code.

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.

当我输入低于 18 岁的值时,我希望程序做出响应:

When I enter a value below 18 as the age, I expect the program to respond:

ACCESS DENIED
Reason:You are way to young

但我没有得到任何输出.为什么?

but I don't get any output. Why?

推荐答案

有时文本缩进可以帮助您查看问题.这是添加了缩进的代码:

Sometimes text indentation helps you to see the issue. Here's your code with indentation added:

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.

为了使实现的逻辑更加明显,我现在将表示嵌套的 if ,而没有它们执行的代码:

And to make the implemented logic more obvious, I will now represent the nested ifs without the code that they execute:

  if age>18 then
    if age<100 then
      ...  // Access Granted
    else
      if age<18 then
        ...  // You are way too young
      else
        ...  // You are way too old
  ;

现在很容易看出标记为 You are way too young 的分支永远不会到达.它应该在 age 小于 18 时执行,但是该 if 语句嵌套在另一个 if 中,它只会在 age 大于 18.因此,age 应该首先大于 18,然后小于 18 才能执行该分支——您现在可以明白为什么不这样做了得到预期的结果!

It is easy to see now that the branch marked as You are way too young is never reached. It is supposed to be executed when age is less than 18, but that if statement is nested into another if which will call it only when age is greater than 18. So, age should first qualify as greater than 18, then less than 18 in order for that branch to execute – you can see now why you do not get the expected result!

预期的逻辑可能以这种方式实现:

The intended logic could possibly be implemented this way:

  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

  ;

我相信您应该能够正确填写缺失的代码块.

I believe you should be able to fill in the missing code blocks correctly.

最后一点:您可能希望将 age>18 更改为 age>=18,这样 18 岁就不会被视为太年轻".

Just one last note: you might want to change age>18 to age>=18, so that 18 proper does not qualify as "too young".

这篇关于帕斯卡比较错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆