是否有可能編寫出要求用戶輸入數字是平均計算平均值的程序?計算平均值用perl
我寫了一個計算三個數字的平均值Perl腳本。這裏是我的代碼:
#usr/bin/perl
use strict;
use warnings;
my $a; #variable declaration
my $b; #variable declaration
my $c; #variable declaration
my $avg; #variable declaration
my $x; #variable declaration
my $y; #variable declaration
my $z; #variable declaration
my $results; #variable declaration
my $number; #variable declaration
$a = 2; #number 1
$b = 6; #number 2
$c = 7; #number 3
$avg = avg($a,$b,$c); #Three variables to be averaged
sub avg {
($x,$y,$z) = @_; #Store variables in array
$results = ($x+$y+$z)/3; #Values stored added, and divided for average
return $results; #return value
}
print "$avg\n";
exit;
相反的數字我進入我的變量而被提示在終端輸入三個數字的我的代碼計算平均值進行平均。我知道在Perl做這樣的事情,你必須執行一些代碼,就像這樣:
print STDOUT "Enter a number: \n";
$averages = <STDIN>;
print "The Average is $averages.\n";
當我加入這個我的代碼沒有打印任何東西了這可怎麼正確實施,以我的代碼。
如果你讀學習的Perl的前四章,你就可以回答這個問題你自己。 –
不幸的是我不得不投票結束這個問題。請閱讀一些優秀的Perl文檔(使用'perldoc'工具或http://perldoc.perl.org)[perlintro](perldoc.perl.org/perlintro.html)開始。對於「真正的」書籍,請嘗試學習Perl(如bdf所述)或免費提供的Chromatic的[Modern Perl](http://www.onyxneon.com/books/modern_perl/index.html)。甚至有Gabor的[教程](http://szabgab.com/perl_tutorial.html)。學習完語言基礎知識後,我們將很樂意幫助您解決錯誤/問題。 –