2012年9月14日金曜日

開発環境

『初めてのPerl 第6版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2012年、ISBN978-4-87311-567-2) の10章(さまざまな制御構造)、10.11(練習問題)1を解いてみる。

その他参考書籍

1.

コード(TextWrangler)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.012;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my $secret_num = int(1 + rand 100);
print "1から100までの秘密の数当てゲーム\n";
print "数値を入力\n";
while(1){
  chomp(my $num = <STDIN>);
  last if $num =~/^\s*$/;
  print "Too high\n" if $num > $secret_num;
  print "Too low\n" if $num < $secret_num;
  if($num == $secret_num){
    print "Secret Number!\n";
    last;
  }
}

入出力結果(Terminal)

$ ./sample.pl
1から100までの秘密の数当てゲーム
数値を入力
50
Too low
75
Too high
62
Too high
55
Too high
52
Too low
53
Too low
54
Secret Number!
$ ./sample.pl
1から100までの秘密の数当てゲーム
数値を入力

$

0 コメント:

コメントを投稿