Chinaunix

标题: Perl疑惑 [打印本页]

作者: xinxinghe    时间: 2014-07-14 18:36
标题: Perl疑惑
小弟现在刚刚开始学习Perl,在看《Perl语言入门学习》第10章做练习题时,题目如下附件。
这是我写的程序:
#!D:\perl64\bin\perl.exe -w
$secret = int(1 + rand 100);
print "请输入1到100的数字:\n";
while((chomp($input = <STDIN>)) ne $secret) {
        print "Too hight!\n"         if $input>$secret;
        print "Too low!\n"        if $input<$secret;
        last if ($input =~ /(quit|exit|^\s+$)/);
}
print "The secret number is: $secret";
<>;
这个程序每当输入了和随机数一样大小的数后就卡了。
这是什么原因啊?

abc.png (104.38 KB, 下载次数: 65)

abc.png

作者: q1208c    时间: 2014-07-14 18:54
回复 1# xinxinghe

while(chomp($input = <STDIN>)) {
        print "Too hight!\n"         if $input>$secret;
        print "Too low!\n"        if $input<$secret;
        last if ($input = $secret);
        last if ($input =~ /(quit|exit|^\s+$)/);
}
print "The secret number is: $secret";

   
作者: stanley_tam    时间: 2014-07-14 19:19
按一下enter就不卡了{:3_193:}
作者: xinxinghe    时间: 2014-07-14 19:31
楼上2位大哥,那我把判断条件写在while后的()里,为什么会卡呢?

作者: xinxinghe    时间: 2014-07-14 19:34
那就按了2下回车了。当没猜对的时候按一下就行了。回复 3# stanley_tam


   
作者: jasanjun    时间: 2014-07-14 23:30
chomp($input = <STDIN>)   

chomp 成功 返回 1 , 失败返回0
作者: xinxinghe    时间: 2014-07-15 08:37
谢谢啊,原来是这样。并不是真的卡了,只是没输出结果,再等待输入呢。回复 6# jasanjun


   
作者: shijiang1130    时间: 2014-07-16 16:16
q1208c 发表于 2014-07-14 18:54
回复 1# xinxinghe

while(chomp($input = )) {


为嘛你的不放在{CODE}里没有乱码
作者: q1208c    时间: 2014-07-16 17:04
回复 8# shijiang1130

这说明我原来就有良好的格式
   




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2