• 复利计算2.0


    mport java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class fulijisuanqi02 {
         static Scanner scanner = new Scanner(System.in);
            static DecimalFormat df = new DecimalFormat("####0.0000");
    
            public static void main(String[] args) {
                //TODO Auto-generated method stub
    
                System.out.println("1.复利计算;");
                System.out.println("2.单利计算;");
                System.out.println("3.本金计算;");
                System.out.println("4.年限计算。");
                System.out.println("5.报酬率计算:");
                System.out.println("6.累计资产总值计算:");
                System.out.println("请选择:");
    
                int k = scanner.nextInt();
                switch (k) {
                case 1:
                    fulijisuan();
                    break;
                case 2:
                    danlijisuan();
                    break;
                case 3:
                    benjinjisuan();
                    break;
                case 4:
                    nianxianjisuan();
                    break;
                case 5:
                    baochoulvjisuan();
                    break;
                case 6:
                    leijizichanjisuan();
                    break;
                default:
                    break;
                }
            }
    
            private static void leijizichanjisuan() {
                // TODO Auto-generated method stub
                System.out.println("请输入本金:");
                double P = scanner.nextDouble();
                System.out.println("请输入每年的投资金额:");
                double T = scanner.nextDouble();
                System.out.println("请输入投资年限:");
                int n = scanner.nextInt();
                System.out.println("请输入投资利率:");
                double i = scanner.nextDouble();
                for(int j = 0;j < n;j++){
                    P = P * (1 + i);
                    P = P + T;
                }
                double F = P - T;
           String f = df.format(F); System.out.println(
    "资产总值是:"+f); } private static void baochoulvjisuan() { // TODO Auto-generated method stub System.out.println("请输入本金:"); double P = scanner.nextDouble(); System.out.println("请输入复利终值:"); double F = scanner.nextDouble(); System.out.println("请输入年限:"); int n = scanner.nextInt(); double i = Math.pow((F / P), (1.0 / n)) - 1;
         String f = df.format(i); System.out.println(
    "利率是:"+f); } private static void nianxianjisuan() { //TODO Auto-generated method stub System.out.println("请输入本金:"); double P = scanner.nextDouble(); System.out.println("请输入利率:"); double i = scanner.nextDouble(); System.out.println("请输入复利终值:"); double F = scanner.nextDouble(); int n = (int) (Math.log(F / P) / Math.log(1 + i)); System.out.println("年限是:" + n); } private static void benjinjisuan() { //TODO Auto-generated method stub System.out.println("请输入复利终值:"); double F = scanner.nextDouble(); System.out.println("请输入利率:"); double i = scanner.nextDouble(); System.out.println("请输入年限:"); int n = scanner.nextInt(); double P; P = F / Math.pow(1 + i, n); String p = df.format(P); System.out.println("本金是:" + p); } private static void danlijisuan() { //TODO Auto-generated method stub System.out.println("请输入本金:"); double P = scanner.nextDouble(); System.out.println("请输入利率:"); double i = scanner.nextDouble(); System.out.println("请输入年限:"); int n = scanner.nextInt(); double F; F = P * (1 + i * n); String f = df.format(F); System.out.println("单利终值是:" + f); } private static void fulijisuan() { System.out.println("请输入本金:"); double P = scanner.nextDouble(); System.out.println("请输入利率:"); double i = scanner.nextDouble(); System.out.println("请输入年限:"); int n = scanner.nextInt(); double F; F = P * Math.pow(1 + i, n); String f = df.format(F); System.out.println("复利终值是:" + f); } }

    该程序我使用的是java语言来进行编译的,代码行数大概有150行左右,工作量大概用了一个小时。

    本次改进预计应该可以在20分钟内完成,代码行数大概需要20-30行。

    本次的修改主要增加了两个功能,一个是计算报酬率,另一个是计算在投资的同时每年再投入一定的金额,这两个的难点主要在于计算的过程,这也是这个程序的难点所在,还有不足的地方就是没有做出界面。

    本次的工作量也超过了预期,主要原因在于计算方面花了比较大的时间,其他地方都是和之前的一样,主要就是计算的方法不同。

  • 相关阅读:
    c/c++ 继承与多态 容器与继承2
    c/c++ 继承与多态 容器与继承1
    c/c++ 继承与多态 静态调用与动态调用
    c/c++ 继承与多态 子类隐藏父类的同名非虚函数
    mysql 开发进阶篇系列 26 数据库RPM安装演示
    mysql 开发进阶篇系列 25 数据库RPM安装目录介绍
    mysql 开发进阶篇系列 24 查询缓存下
    (后端)org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1,actual 0
    (后端)sql server 按时间段查询
    (网页)javascript如何用递归写一个简单的树形结构
  • 原文地址:https://www.cnblogs.com/wangzekai/p/5282144.html
Copyright © 2020-2023  润新知