复利计算--单元测试

@Test
    public void test0() {//测单利终值
        double money=1000000.0;
        int years=30;
        double r=0.03;
        double sum;
        sum = money + money * years * r;
         Assert.assertEquals(1900000,sum, 1.0);
    }
    @Test
    public void test1(){//测复利终值
        double money=1000000.0;
        int years=30;
        double r=0.03;
        double N=1;
        double sum;
        for (int i = 1; i <= years; i++) {
            N = (1 + r) * N;
        }
        sum = N * money;
        Assert.assertEquals(2427262,sum, 1.0);
    }
    @Test
    public void test2(){//测本金
         double N = 1, money;
         double sum=3000000;
         int years=30;
         double r=0.03;
         for (int i = 1; i <= years; i++) {
             N = (1 + r) * N;
         }
         money = sum / N;
         Assert.assertEquals(1235960,money, 1.0);
    }

 

请教了一些同学 但还是一知半解

posted @ 2016-04-01 11:11  38.曾昱霖  阅读(164)  评论(0编辑  收藏  举报