作者xup6yvu06 (JOY)
看板Perl
标题[问题]新手提问-计算复利问题(subroutine)
时间Sun Jul 15 14:27:11 2018
不好意思!我已反覆看了这个程式许多次还是搞不懂哪里出了问题,
麻烦板上高手解惑。
我的困恼是,第二个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://webptt.com/cn.aspx?n=bbs/Perl/M.1531636033.A.8C2.html
1F:→ edwar: 也许出错的不是第二个函式,是喂给它的值本身就有问题 07/15 18:40
2F:→ jkchang: $inRate 还是 $intRate ? 07/16 10:07
3F:推 clanguage: 你打错字了 07/16 15:10