開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9) の10章(さまざまな制御構造), 10.11(練習問題)1を解いてみる。
1.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl use strict; use warnings; my $secret = int(1 + rand 100); print "1から100までの秘密の数当て\n"; while(1){ chomp(my $num = <STDIN>); last if $num =~ /^(\s*|quit|exit)$/; if($num == $secret){ print "secret\n"; last; } elsif ($num > $secret){ print "Too high\n"; }else{ print "Too low\n"; } }
入出力結果(Terminal)
$ ./sample.pl 1から100までの秘密の数当て exit $ ./sample.pl 1から100までの秘密の数当て quit $ ./sample.pl 1から100までの秘密の数当て $ ./sample.pl 1から100までの秘密の数当て 50 Too low 75 secret $ ./sample.pl 1から100までの秘密の数当て 50 Too low 75 Too low 88 Too high 82 Too high 79 secret $
0 コメント:
コメントを投稿