第十章:其他控制结构

上一篇 / 下一篇  2008-11-12 11:36:08 / 个人分类:《Perl语言入门》-小骆驼书-练习题答案

1)
use warnings;
my $secretNum = int(1 + rand 1000);
my $num;
my $count = 0;
while(1){
 print "Please enter a number from 1 to 1000: ";
 chomp($num = <STDIN>);
 last if $num =~ /exit|quit|^\s*$/i;
 $count++;
 if ($num =~ /[\D]/){
  print "Number!!!\n";
  redo;
 }
 if ($num > 1000 or $num < 1){
  print "1..1000\n";
  redo;
 }
 if ($num < $secretNum){
  print "Too low! You have tried $count times.\n";
 } elsif ($num > $secretNum){
  print "Too high! You have tried $count times.\n";
 } else{  
  print "Bingo! You have tried $count times.\n";
  last;
 }
}


TAG:

 

评分:0

我来说两句

日历

« 2024-04-28  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 47783
  • 日志数: 80
  • 建立时间: 2008-10-27
  • 更新时间: 2009-07-17

RSS订阅

Open Toolbar