在我的代碼中,當我在輸入中輸入一些非數字字母(即$temp
)時,它以「太冷!」作出響應。而不是「無效」。我錯過了什麼?IF語句意外的結果:「string」<= 72是真的?
#!/usr/bin/perl
print "What is the temperature outside? ";
$temp=<>;
if ($temp > 72) {
print "Too hot!\n"; }
elsif ($temp <= 72) {
print "Too cold!\n"; }
else {
print "Temperature $temp is invalid.\n"; }
Perl是這方面的「弱類型」語言。操作人員要小心,以免受到強制(如此!)咬。 – 2011-12-12 02:49:47