【发布时间】:2018-09-03 23:00:16
【问题描述】:
我试图让我的 if 语句捕捉到一个人输入的输入说退出或退出,但事实并非如此。这是我的代码
use strict;
use warnings;
my $g=0;
my $rn = int(rand(25));
until ($g == $rn) {
$g = <STDIN>;
if ($g == $rn) {
print "You got it";
} elsif (chomp($g) eq "quit" || chomp($g) eq "exit") {
print "it triggered";
} elsif ($g > $rn) {
print "incorrect its lower";
} elsif ($g <$rn) {
print "incorrect its higher";
} else {
print "end";
}
}
}
elsif (chomp($g) eq "quit" || chomp($g) eq "exit) {
尽管多次尝试捕获错误,但行未捕获。我试过打印程序看到的内容无济于事。当我从严格/警告中输入退出时,我得到的回应是
argument "quit\n" isn't numeric in numeric eq (==) at ./program24.pl line 30, <STDIN> line 1.
argument "quit" isn't numeric in numeric gt (>) at ./program24.pl line 36, line 1.
我查看了其他几篇关于此的帖子,但似乎没有任何内容是导致此问题的原因。我做错了什么?
【问题讨论】:
-
1)
chomp不返回切碎的字符串;它修改了它的论点。 2)在数值比较之前做字符串比较。 3) 你不处理 EOF (!defined($g))。