Pascal 中带有逻辑运算符(< > = 等)的 Case 语句

发布于 2024-08-04 06:37:19 字数 376 浏览 5 评论 0 原文

我在做这项工作时遇到了麻烦。显然,我不能使用 >或<在 case 语句中,有解决方法吗?谢谢!

case num of
    0:
        begin
            cont_0 := cont_0 + 1;
        end;
    > 0:
        begin
            cont_pos := cont_pos + 1;
            sum_pos  := sum_pos + num;
        end;
    < 0:
        begin
            sum_neg := sum_neg + num;
        end;  
    else;
end;

I'm having trouble making this work. Apparently, i can't use > or < in the case sentence, is there a workaround for this? Thanks!

case num of
    0:
        begin
            cont_0 := cont_0 + 1;
        end;
    > 0:
        begin
            cont_pos := cont_pos + 1;
            sum_pos  := sum_pos + num;
        end;
    < 0:
        begin
            sum_neg := sum_neg + num;
        end;  
    else;
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵情客 2024-08-11 06:37:19
case Sign(num) of
    -1: ... 
     0: ...
     1: ...
end;

if ... else if ... else 更具可读性?你决定。

case Sign(num) of
    -1: ... 
     0: ...
     1: ...
end;

More readable than if ... else if ... else? You decide.

通知家属抬走 2024-08-11 06:37:19

那么不使用case,为什么不使用if呢?

if num = 0 then
        cont_0 := cont_0 + 1;
if num > 0 then
BEGIN
        cont_pos := cont_pos + 1;
        sum_pos  := sum_pos + num;
END
if num < 0 then
        sum_neg := sum_neg + num;

Don't use case then, why not use if?

if num = 0 then
        cont_0 := cont_0 + 1;
if num > 0 then
BEGIN
        cont_pos := cont_pos + 1;
        sum_pos  := sum_pos + num;
END
if num < 0 then
        sum_neg := sum_neg + num;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文