编写一个Java程序来计算复合利息,并附带示例。复合利息计算的背后公式
未来总额 = 本金 * ( 1 + 利率 )年数)
上述公式用于计算未来总额,因为它同时包含了本金和复合利息。要获得复合利息,请使用以下公式
复合利息 = 未来总额 – 本金
Java 复合利息计算示例程序
此程序允许用户输入本金、利率和总年数。通过使用这些值,该程序使用上述公式计算复合利息。
import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args)
{
double PAmount, ROI, TimePeriod, FutureCI, CI;
sc = new Scanner(System.in);
System.out.print(" Please Enter the Principal Amount : ");
PAmount = sc.nextDouble();
System.out.print(" Please Enter the Rate Of Interest : ");
ROI = sc.nextDouble();
System.out.print(" Please Enter the Time Period in Years : ");
TimePeriod = sc.nextDouble();
FutureCI = PAmount * (Math.pow(( 1 + ROI/100), TimePeriod));
CI = FutureCI - PAmount;
System.out.println("\n The Future for Principal Amount " + PAmount + " is = " + FutureCI);
System.out.println(" The Actual for Principal Amount " + PAmount + " is = " + CI);
}
}

使用函数计算复合利息的程序
此程序与上面相同。但这次,我们将创建一个单独的方法来计算复合利息。
import java.util.Scanner;
public class Example {
private static Scanner sc;
public static void main(String[] args)
{
double PAmount, ROI, TimePeriod;
sc = new Scanner(System.in);
System.out.print(" Please Enter the Principal Amount : ");
PAmount = sc.nextDouble();
System.out.print(" Please Enter the ROI : ");
ROI = sc.nextDouble();
System.out.print(" Please Enter the Time Period in Years : ");
TimePeriod = sc.nextDouble();
calCmpI(PAmount, ROI, TimePeriod);
}
public static void calCmpI(double PAmount, double ROI, double TimePeriod)
{
double FutureCI, CI;
FutureCI = PAmount * (Math.pow(( 1 + ROI/100), TimePeriod));
CI = FutureCI - PAmount;
System.out.println("\n Future for " + PAmount + " is = " + FutureCI);
System.out.println(" Actual for " + PAmount + " is = " + CI);
}
}
Please Enter the Principal Amount : 1400000
Please Enter the ROI : 9.5
Please Enter the Time Period in Years : 7
Future for 1400000.0 is = 2642572.2488883743
Actual for 1400000.0 is = 1242572.2488883743