复利计算1.0,2.0,3.0(java)
程序源代码:
import java.util.Scanner; public class ch { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("复利计算器:计算复利终值请按1,计算本金请按2;股票投资:计算时间请按3,计算报酬率请按4,计算资产总值请按5" ); int num=scanner.nextInt(); //选择计算对象单利终值 if (num == 1) { double N = 1, sum; System.out.print("请输入本金:"); double money = scanner.nextDouble(); System.out.print("请输入存钱年数:"); int years = scanner.nextInt(); System.out.print("请输入利率:"); double r = scanner.nextDouble(); sum = money + money * years * r; System.out.println("单利终值为:" + sum); sum = 0; for (int i = 1; i <= years; i++) { N = (1 + r) * N; } sum = N * money; System.out.println("复利终值为:" + sum); } //选择计算对象应投入的本金 if (num == 2) { double N = 1, money; System.out.print("请输入复利终值:"); double sum = scanner.nextDouble(); System.out.print("请输入存钱年数:"); int years = scanner.nextInt(); System.out.print("请输入利率:"); double r = scanner.nextDouble(); for (int i = 1; i <= years; i++) { N = (1 + r) * N; } money = sum / N; System.out.println("应投入的本金为:" + money); } //选择计算对象需要时间 if (num == 3) { double years; System.out.print("请输入本金:"); double money = scanner.nextDouble(); System.out.print("请输入最后得到资产总值:"); double sum = scanner.nextDouble(); System.out.print("请输入利率:"); double r = scanner.nextDouble(); years = sum / (money * r); System.out.println("时间为(年):" + years); } //选择计算对象利率 if (num == 4) { double r; System.out.print("请输入本金:"); double money = scanner.nextDouble(); System.out.print("请输入最后得到资产总值:"); double sum = scanner.nextDouble(); System.out.print("请输入存钱年数:"); int years = scanner.nextInt(); r=sum/(money*years); System.out.println("利率为:" + r); } //选择计算对象利率资产总值 if (num == 5){ double sum; System.out.print("请输入本金:"); double money = scanner.nextDouble(); System.out.print("请输入利率:"); double r = scanner.nextDouble(); System.out.print("请输入存钱年数:"); int years = scanner.nextInt(); sum=money*r*years; System.out.println("若投资本金固定不变,则资产总值为:" + sum); sum=0; for(int i=1;i<=years;i++){ sum=money*r*1; money=sum; } System.out.println("若连本带利投资,则资产总值为:" + sum); } } }