猜数字游戏:设计裸块,redo,和字符串的判断

下面是正确的程序:

$secretnumber=int(1 + rand 100);
print "$secretnumber\n";
{#一个裸块
print "Please guess a number from one to hushand!\n";
chomp($gussnumber=<STDIN>);
if ($gussnumber=~/quit|exit|\A\s*\z/i)#必须要放在前面,如果放在后面字符创会被当做0
 {
	 last;
 }

elsif ($secretnumber<$gussnumber)
{
   print "Too hight\n";
	 redo;
 }
 elsif ($secretnumber>$gussnumber)
 {
	 print "Too low.\n";
	 redo;
 }
 elsif ($secretnumber==$gussnumber)
 {
	 print "You win!\n";
	 last;
 }
 }

以下为实验结果:

F:\>perl\a.pl
70
Please guess a number from one to hushand!
50
Too low.
Please guess a number from one to hushand!
310
Too hight
Please guess a number from one to hushand!
72
Too hight
Please guess a number from one to hushand!
quit

F:\>

如果把quit那一部分放在后面,如下:

$secretnumber=int(1 + rand 100);
print "$secretnumber\n";
{#一个裸块
print "Please guess a number from one to hushand!\n";
chomp($gussnumber=<STDIN>);

if ($secretnumber<$gussnumber)
{
   print "Too hight\n";
	 redo;
 }
 elsif ($secretnumber>$gussnumber)
 {
	 print "Too low.\n";
	 redo;
 }
 elsif ($secretnumber==$gussnumber)
 {
	 print "You win!\n";
	 last;
 }
 elsif ($gussnumber=~/quit|exit|\A\s*\z/i)#放在后面的效果
 {
	 last;
 }

 }

如果你输入quit或者exit等得到的永远都只是一个结果:

F:\>perl\a.pl
44
Please guess a number from one to hushand!
quit
Too low.
Please guess a number from one to hushand!




你可能感兴趣的:(猜数字游戏:设计裸块,redo,和字符串的判断)