[問題]新手提問-計算複利問題(subroutine)

看板Perl作者 (JOY)時間5年前 (2018/07/15 14:27), 編輯推噓1(102)
留言3則, 3人參與, 5年前最新討論串1/1
不好意思!我已反覆看了這個程式許多次還是搞不懂哪裡出了問題, 麻煩板上高手解惑。 我的困惱是,第二個subroutine無法計算出正確數值,得出的值永遠是$principal 這個輸入值。謝謝! sub getInfo { print " What is the principal investment amount?\n"; chomp ($principal = <STDIN>); print "What is the anual interest rate?\n"; chomp ($intRate = <STDIN>); print "How many times is the interest compound per year?\n"; chomp ($num = <STDIN>); print "How many year is the money invested or borrowed for?\n"; chomp ($years = <STDIN>); ($principal, $inRate, $num, $years); } sub compoundInterest { my $P = shift; my $r = shift; my $r = $r/100; my $n = shift; my $t = shift; $value = $P*(1+$r/$n)**($n*$t); return $value; } $answer = "y"; while ($answer eq "y") { ($principal, $inRate, $num, $years) = &getInfo; $value = &compoundInterest($principal, $inRate, $num, $years); print "The value of your investment after $years years will be $value.\n"; print "Would you like to run another compound interest calculation? (y/n)\n"; chomp ($answer = <STDIN>); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 47.186.192.177 ※ 文章網址: https://www.ptt.cc/bbs/Perl/M.1531636033.A.8C2.html

07/15 18:40, 5年前 , 1F
也許出錯的不是第二個函式,是餵給它的值本身就有問題
07/15 18:40, 1F

07/16 10:07, 5年前 , 2F
$inRate 還是 $intRate ?
07/16 10:07, 2F

07/16 15:10, 5年前 , 3F
你打錯字了
07/16 15:10, 3F
文章代碼(AID): #1RIkb1Z2 (Perl)