作者giacch (小a)
看板Perl
标题Re: [问题] for loop里的一个小问题
时间Sat Oct 4 00:33:04 2008
※ 引述《yingwan (yingwan)》之铭言:
: 大家好
: 最近在写回圈时又遇到一个问题了
: 我想要让每个使用者有五次输入幸运数字的机会
: 输入五次後系统会说你输入第几次是对的数字(如果你猜对幸运数字)
: 如果都没猜对,系统就会你都没猜中
: 但是有个情境我一直无法写好
: 假设A五次中不只猜中一次,他第一次跟最後一次都猜中
: 但我的程式只能显示他猜中的最後一次,这到底要怎麽修正啊?
: 我想了好几天都想不太出来,希望各位大大可以给我一点意见,谢谢
: 我把我的指令贴在下面以供大家参考
: #!usr/bin/perl
: #-----------------------------------------------
: #Write a program that asks users to guess
: #the lucky number five times using a for loop.
: #-----------------------------------------------
: for ($count=0; $count<5; $count++){
: print "Please enter your lucky number.\n";
: chomp ($num=<STDIN>);
: if ($num==8514) {$flag=$count+1; next;}
: }
: $f=($flag);
: if ($flag!=0) {print "Great! You guessed the lucky number in guess #$f.\n";}
: else {print "All your guesses were wrong. Goodbye.\n";}
#!usr/bin/perl
#-----------------------------------------------
#Write a program that asks users to guess
#the lucky number five times using a for loop.
#-----------------------------------------------
$Ans = 8514;
undef @flag;
for ($count=0; $count<5; $count++){
print "Please enter your lucky number.\n";
chomp ($num=<STDIN>);
# if ($num==8514) {$flag=$count+1; next;}
if($num==$Ans) { push(@flag, $count+1); }
}
#$f=($flag);
#if ($flag!=0) {print "Great! You guessed the lucky number in guess #$f.\n";}
if($#flag != -1) { for(@flag) {
print "Great! You guessed the lucky number in guess #$_.\n"; } }
else {print "All your guesses were wrong. Goodbye.\n";}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.232.236.185
1F:推 yingwan:没想到要用array啊..强!谢谢大大 10/04 06:43