Perl If 和 Elsif 循环帮助

发布于 2024-10-30 19:22:09 字数 846 浏览 0 评论 0原文

好吧,我还是 Perl 的新手,所以这个问题的答案可能看起来相当明显,但我在 Python 中做了一些工作,并且在学习 if、elsif 和 else 循环时遇到了问题;具体来说,它们无法正常工作。这是我的代码:

my $x = 0;
print "X has been set to ". $x .".\n";

while ($x<11)
{
  $x++;
  print "The value of x is now ". $x .".\n";
  if ($x>4, $x<7){
      print "Something\n";
      system ("Pause");
  }

  elsif ($x>7, $x>11){ #<--Here
  print "Something else\n";
  system ("Pause");
  }

  elsif ($x==11){
      print "Last line\n";
  }
} #<-- and Here
system "Pause";

也许我的问题现在很明显,但如果不是,问题是它似乎没有评估任何表达式;它只是打印它找到的第一个循环,在本例中是 if 循环。如果我删除它,或者注释掉它,它会直接进入第一个 elsif 循环;也就是说,无论 x 的值是什么,它都会打印它找到的第一个循环,而不进行任何评估。当我添加时,

use strict;
use warnings;

我收到警告“在第 16 行的 void context 中无用使用数字 gt (>)”,第 24 行也是如此。我在原始代码中用箭头标记了这些警告还有这里。我是否在做某事/没有做我应该做的事?

Alright, I'm still a newbie in Perl, so the answer to this question may seem fairly obvious, but I've done some work in Python, and I've encountered a problem with learning the if, elsif, and else loops; specifically, that they don't work properly. Here's my code:

my $x = 0;
print "X has been set to ". $x .".\n";

while ($x<11)
{
  $x++;
  print "The value of x is now ". $x .".\n";
  if ($x>4, $x<7){
      print "Something\n";
      system ("Pause");
  }

  elsif ($x>7, $x>11){ #<--Here
  print "Something else\n";
  system ("Pause");
  }

  elsif ($x==11){
      print "Last line\n";
  }
} #<-- and Here
system "Pause";

Maybe my problem is obvious by now, but if not, the problem is that it doesn't seem to be evaluating any of the expressions; it just prints the first loop it finds anyway, which in this case is the if loop. If i remove it, or comment it out, it goes straight to the first elsif loop; that is, no matter the value of x, it prints the first loop it finds without any sort of evaluation. When I added

use strict;
use warnings;

I got the warning "Useless use of numeric gt (>) in void context at line 16" and the same thing for line 24. I marked those out in the original code with the arrows and here. Am I doing something/not doing something I should be?

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

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

发布评论

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

评论(2

铃予 2024-11-06 19:22:09
use strict;
use warnings;

while ($x<11)
{
  $x++;
  print "The value of x is now ". $x .".\n";
  if ($x>4 and $x<7){
      print "Something\n";
      system ("Pause");

  }elsif ($x>7 and $x>11){ #<--Here
    print "Something else\n";
    system ("Pause");

  }elsif ($x==11){
      print "Last line\n";
  }
} #<-- and Here
system "Pause";
use strict;
use warnings;

while ($x<11)
{
  $x++;
  print "The value of x is now ". $x .".\n";
  if ($x>4 and $x<7){
      print "Something\n";
      system ("Pause");

  }elsif ($x>7 and $x>11){ #<--Here
    print "Something else\n";
    system ("Pause");

  }elsif ($x==11){
      print "Last line\n";
  }
} #<-- and Here
system "Pause";
浮生未歇 2024-11-06 19:22:09

if (expr1, expr2) 将忽略 expr1。您可能指的是 and&& 吗? (此外,如果是这样,您的 elsif 应该测试 x<11。)

if (expr1, expr2) will ignore expr1. Did you perhaps mean and or &&? (Also, if so, your elsif should be testing x<11.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文