【问题标题】:perl - Trying to use a while loop to ask the user if they want to do that againperl - 尝试使用 while 循环询问用户是否想再次这样做
【发布时间】:2016-10-22 15:10:44
【问题描述】:

(perl 新手) 我有一个计算阶乘的小型 perl 程序。我想使用一个while循环,这样在用户得到结果后,他们会被问到“计算另一个阶乘?Y/N”,然后让 Y 再次运行代码并让 N 结束程序。

这是我的代码:

print"Welcome! Would you like to calculate a factorial? Y/N\n";

$decision = <STDIN>;

while $decision == "Y";
{
    print"Enter a positive # more than 0: \n";

    $num = <STDIN>;
    $fact = 1;

    while($num>1)
    {
        $fact = $fact * $num;
        $num $num - 1;
    }

    print $fact\n;
    print"Calculate another factorial? Y/N\n";
    $decision = <STDIN>;
}
system("pause");

给我带来麻烦的是在哪里放置 while 循环以及如何使 Y/N 选项起作用。我也不清楚system("pause")sleep 功能。我确实知道system("pause") 可以让我的程序正常运行。

【问题讨论】:

  • 需要chomp,而== 是一个数字比较——你可能需要eq
  • 当我运行它时,它会立即自行关闭,尽管system("pause"); 看代码你知道为什么会这样吗?
  • 当我尝试你的代码时,我得到了Scalar found where operator expected at err.pl line 15, near "$num $num" (Missing operator before $num?) Backslash found where operator expected at err.pl line 18, near "$fact\" (Missing operator before \?) syntax error at err.pl line 5, near "while $decision " syntax error at err.pl line 15, near "$num $num " Execution of err.pl aborted due to compilation errors.,也就是说,Perl 甚至没有机会考虑你的system("pause") 行。
  • 如前所述,Enter a positive # more than 0: 应该是Enter a non-negative integer:。零是一个完美的 valid 输入,您无需更改任何其他内容即可工作!

标签: perl loops factorial


【解决方案1】:

您的程序几乎是正确的,只是有几个问题:

  1. 请习惯始终将use strict;use warnings; 添加到您的脚本中。 他们会 (除其他外)强制您声明您使用的所有变量(使用my $num=…;) 并警告您常见错误(如拼写错误)。有些人认为这是一个错误 use strict;use warnings; 默认不开启。
  2. 从 STDIN(或其他文件句柄)读取一行时,读取的行将包含 尾随换行符“\n”。为了你的比较工作,你必须摆脱它使用 chomp 函数。
  3. Perl 中有两组不同的比较运算符:一组用于字符串,一组用于数字。 将数字与&lt;&gt;&lt;=&gt;===!= 进行比较。对于必须使用的字符串 lt(小于)、gtle(小于或等于)、geeqne。如果您使用其中一个号码 字符串上的运算符 Perl 将尝试将您的字符串解释为数字,因此 $decision == "Y" 将检查$decision 是否为0。如果你有use warnings; Perl 会注意到你的。 请改用$decision eq "Y"
  4. 外部while 循环在比较之后有一个尾随;,这会给你一个无穷无尽的 循环或无操作(取决于$decision 的内容)。
  5. 您忘记了$num = $num - 1; 中的=
  6. 你忘记了 " 周围的引号 print "$fact\n";
  7. system("pause") 仅适用于 pause 是外部命令的 Windows。在 Linux 上(其中 我刚刚测试过)没有这样的命令,system("pause") 失败,command not found。 我将其替换为 sleep(5);,它只需等待 5 秒。

.

#!/usr/bin/env perl

use strict;
use warnings;

print "Welcome! Would you like to calculate a factorial? Y/N\n";

my $decision = <STDIN>;
chomp($decision);    # remove trailing "\n" from $decision

while ( $decision eq 'Y' ) {
    print "Enter a positive # more than 0: \n";

    my $num = <STDIN>;
    chomp($num);
    my $fact = 1;

    while ( $num > 1 ) {
        $fact = $fact * $num;
        $num  = $num - 1;
    }

    print "$fact\n";
    print "Calculate another factorial? Y/N\n";
    $decision = <STDIN>;
    chomp($decision);
}
print "ok.\n";

sleep(5);    # wait 5 seconds

【讨论】:

  • sleep(5) 不是system("pause") 的合适替代品。此外,这个程序真的不需要。
  • 提示:标准是在提示中使用小写字母,默认除外。这将使提示y/N。另外,这里应该接受小写和大写输入,所以$decision eq 'Y' 应该是fc($decision) eq 'y'
  • 提示:--$num; 可以替代$num = $num - 1;
  • 提示:while ( $num &gt;= 2 ) 会比 while ( $num &gt; 1 ) 更清楚一点
  • @ikegami 您所有的 cmets 都是完全有效的。确实,还有改进的余地。我只是想给出一个与 OP 差异尽可能小的答案。我认为sleeppause 应该只阻止Windows 在双击脚本时立即关闭窗口,以便可以看到它的输出。
【解决方案2】:

始终将use warningsuse strict 添加到程序的开头。 您的代码中有许多错别字会被此捕获。

#!/usr/bin/perl
use warnings;
use strict;


print "Welcome! Would you like to calculate a factorial? Enter 'Y' or 'N': ";

my $answer = <STDIN>;
chomp($answer);

while($answer =~ /^[Yy]$/){
    my $fact = 1;
    print"Enter a positive number greater than 0: ";

    my $num = <STDIN>;
    chomp($num);
    my $number_for_printing = $num;

    while($num > 0){
        $fact = $fact * $num;
        $num--;
    }
    print "The factorial of $number_for_printing is: $fact\n";

    print"Calculate another factorial? Enter 'Y' or 'N': ";
    $answer = <STDIN>;
    chomp($answer);
}

print "Goodbye!\n";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-19
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 2016-09-22
    相关资源
    最近更新 更多