开始Perl语法错误

时间:2015-03-15 13:30:02

标签: perl

感谢您在寻找问题时通过网络搜索我在本网站上找到的帮助。

它接缝我无法弄清楚这段代码的问题是什么。有人告诉我,Perl比Bash容易,但现在我已经尝试过了,我不确定这是否属实。无论如何......我已经完成了这个代码并进行了搜索但却未能发现它有什么问题。部分问题在于,我的旧大脑并不像以前那样容易学习新事物。代码

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

my $target = 12;
my $guess=0;
print "Guess my number!\n";
print "Enter your guess: ";
$guess = <STDIN>;
while ($guess != $target) {
  if ($target == $guess) {
    print "That's it! You guessed correctly!\n";
    exit;
  }
  elsif ($guess > $target) {
    print "Your number is bigger than my number\n";
    print "Try agin";
    print "Enter a lower guess: ";
    $guess = <STDIN>;
  }
  else ($guess < $target) {  # syntax error at , near "else ("
    print "Your number is less than my number\n";
    print "Try agin";
    print "Enter a higher guess: ";
    $guess = <STDIN>;
  }
}  # syntax error at , near "}"

我在上面标记的行上遇到错误。然而,代码与它上面的elsif中的代码相同。

1 个答案:

答案 0 :(得分:6)

语法为else {,因为else没有条件,而elsif确实有条件。语法是:

if (condition) {
    ...
}
elsif (condition) {
    ...
}
else {
    ...
}