跳转至内容

Perl 编程/练习 6 答案

来自维基教科书,开放的书籍,开放的世界

use warnings; use strict;

$| = 1;

print "请输入游戏次数:"; chomp(my $Num_Games = <STDIN>);

my $Num_Guesses = 0; for my $gameno (1 .. $Num_Games) {

       my $number = 1 + int rand 100;

       my $guess;
       do {
               print "Enter guess from 1 to 100: ";
               chomp($guess = <STDIN>);
               ++$Num_Guesses;

               if ($guess < $number) {
                       print "Higher!\n";
               } elsif ($guess > $number) {
                       print "Lower!\n";
               }
       } until $guess == $number;

       print "Correct!\nAverage guesses per game: ", 
       $Num_Guesses / $gameno, "\n\n";

}

print "已玩游戏次数:$Num_Games\n";

华夏公益教科书