perl 流程控制范例

2010-03-10  赵祥方 

#!/user/bin/perl -w
while (1) {
    $think_number = int(rand 10);
    print "please input a number:\n";
    $guess = <STDIN>;
    chomp $guess;
    $guesstime = 1;
   
    while (1) {
        if ($guess > $think_number) {
            print "guess number too big!\n";
            print "please input again:";
            $guess = <STDIN>;
            chomp $guess;
            $guesstime++;
        }
        elsif ($guess < $think_number) {
            print "guess number too small!\n";
            print "please input again:";
            $guess = <STDIN>;
            chomp $guess;
            $guesstime++;
        }
        else {
            if ($guesstime > 3) {
                print "you are stupid, used $guesstime times to guess!\n";
            }
            else {
                print "you are clever , used $guesstime times to guess!\n";
            }
            last;
        }
    }
   
    print "Try again please enter 1 or Go exit please enter 0?\n";
   
    $IsTryAgain = <STDIN>;
    chomp $IsTryAgain;
    if (not $IsTryAgain) {
        exit;
    }
}
 
如果你的系统安装了perl,把上面的代码保存在.pl文件中,从cmd中进入到文件目录,运行perl  文件名.pl 看看。
368°/3683 人阅读/0 条评论 发表评论

登录 后发表评论