打印出1-100之间的随机数,并且让用户输入,输入正确了退出,失败继续输入。当输入exit,quit,空行的时候打印出随机数字并且退出。
#!/usr/bin/perl
my $rand = int (1+ rand 100);
#print "this is $rand"; #调试程序
while (1) { #建立死循环
print "Please this 1 to 100 number:";
chomp(my $guess = <STDIN>);
if ($guess =~ /quit|exit|^\s*$/i){ #判断条件
print "Number is $run\n";
last; #如果执行成功将退出循环
}
elsif ($guess < $run ){
print "Too small.try again\n";#提示用户输入和正确的数字相差距离
}
elsif ($guess == $run){
print "That right\n";
last;
}
else{
print "too large.try again!\n";
}
}
my $rand = int (1+ rand 100);
#print "this is $rand"; #调试程序
while (1) { #建立死循环
print "Please this 1 to 100 number:";
chomp(my $guess = <STDIN>);
if ($guess =~ /quit|exit|^\s*$/i){ #判断条件
print "Number is $run\n";
last; #如果执行成功将退出循环
}
elsif ($guess < $run ){
print "Too small.try again\n";#提示用户输入和正确的数字相差距离
}
elsif ($guess == $run){
print "That right\n";
last;
}
else{
print "too large.try again!\n";
}
}
本文转自 mcshell 51CTO博客,原文链接:http://blog.51cto.com/mcshell/485800,如需转载请自行联系原作者