【发布时间】:2019-12-08 01:59:33
【问题描述】:
我的目标是创建一个程序,询问用户金额,询问每年、每月或每天的利率,询问其复利方式,然后询问以月、日或年为单位的期限.
然后它会打印未来价值以及获得的总利息。 这是我到目前为止所拥有的,数字不正确。 如果有人可以帮助修改并使其工作,我将非常感激。
import java.util.Scanner;
public class Compunding {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double compoundingTerms;
double period = 0;
System.out.println("Enter an amount of money: ");
double amount = sc.nextDouble();
System.out.println("Enter an rate of Interest: ");
double rate = sc.nextDouble();
System.out.println("Enter per years, months, or days: ");
String time = sc.next();
System.out.println("Enter how it will be componded monthly, semi-anually, quarterlly, anually: ");
String compoundRate = sc.next();
System.out.println("Enter the term amount: ");
double term = sc.nextDouble();
System.out.println("Enter the term type (Monthy,Yearly,Daily}: ");
String termType = sc.next();
if (time.equals("years")) {
period = 1;
}
if (time.equals("months")) {
period = 12;
}
if (time.equals("days")) {
period = 365;
}
if (compoundRate.equals("monthly")) {
rate = (rate / 100) / 12;
term = term * 12;
}
if (compoundRate.equals("semi-anually")) {
rate = (rate / 100) / 2;
term = term * 2;
}
if (compoundRate.equals("quarterlly")) {
rate = (rate / 100) / 4;
term = term * 4;
}
if (compoundRate.equals("anually")) {
rate = rate / 100;
term = term * 1;
}
double compoundPayment = 0;
for (int i = 1; i <= term; i++ ) {
if (i % period == 0 ) {
colInterest(amount, rate);
}
compoundPayment = amount * (1.0 + rate);
}
System.out.println("The Final payment will be: " + compoundPayment);
}
public static double colInterest(double valueAmount, double valueInterest) {
return valueAmount * valueInterest;
}
}
【问题讨论】:
-
colInterest(金额,利率);被调用,但它的值没有分配给任何东西。为什么?
-
compoundPayment = 金额 * (1.0 + 费率);在 for 循环外调用,并将报告的金额设置为仅收集一次利息的数字。在我看来,您的意思是在循环中设置复合付款和/或随时更新金额。
-
谢谢,我继续解决了这个问题。但是,问题仍然存在,我从程序中获得的数字不正确。您将如何编写程序以从我给出的程序描述中输出正确的数字?