用perl写了一个猜数字

2007-07-20 阅读 138

写一个程序,能重复要求用户猜测某个在1 到100 之间的数字,直到猜对为止。你的程序应当能随机的产生一个数字,使用公式int(1 + rand 100)。

当用户猜测错误时,程序应该回应“Too high”或者“Too low”。如果用户输入quit或exit,或者回车时,程序应立即退出。如果用户猜测正确,程序也退出。

#! /usr/bin/perl

while()
{
         chomp;
         print "INPUT: " . $_ . "\n";
         if( $_ eq "" || $_ eq "quit" || $_ eq "exit" )
         {
                 print "It's over. \n";
                 last;
         }
         else
         {
                 $now = int(1 + rand 100);
                 print "now $now \n";
                 my $out =
                 ($_ > $now) ? "Too high~" :
                 ($_ < $now) ? "Too low~" :
                                 "" ;
                 if( $out eq "" )
                 {
                         print "It's ok";
                         last;
                 }
                 else
                 {
                         print $out . "\n";
                 }
         }
}

perl 学习笔记

2016-01-04 23:51:34 我当时是有多无聊啊

更新于 2023年03月28日