终值计算程序(Java实现)

本程序使用Java编写了一个终值计算程序,给定一个固定的银行利率,一个固定的年收入以及一个固定的存款目标,计算多少年后这一目标可以实现。程序输出每一年年末的总存款数额以及实现目标所需要的时间。输出内容分别使用程序台控制输出以及.txt文本输出。本程序使用Java(TM) SE Runtime Environment (build 12.0.2+10),编译器使用IntelliJ IDEA。编写该程序需要使用的Java相关技术为:

  1. Java控制台输入输出;
  2. Java文本写入;
  3. Java循环语句;

程序源码如下:

import java.util.*;
import java.io.*;

public class retire_balance {
    public static void main(String [] args) throws FileNotFoundException, UnsupportedEncodingException {
        PrintWriter outfile = new PrintWriter("bankaccount.txt","UTF-8");
        Scanner in = new Scanner(System.in);
        System.out.println("what's your goal?(dollars)");
        double goal = in.nextDouble();

        System.out.println("How much money do you need to cash in every year?(dollars)");
        double payment = in.nextDouble();

        System.out.println("please input interest in %");
        double interestRate = in.nextDouble();

        double balance = 0.0;
        int years = 0;
        double interest = 0.0;

        while(balance

程序输入如下:

what's your goal?(dollars)
1000000
How much money do you need to cash in every year?(dollars)
100000
please input interest in %
1.5

程序输出如下:

you have 101500.00 dollars after 1 years later.
you have 204522.50 dollars after 2 years later.
you have 309090.34 dollars after 3 years later.
you have 415226.69 dollars after 4 years later.
you have 522955.09 dollars after 5 years later.
you have 632299.42 dollars after 6 years later.
you have 743283.91 dollars after 7 years later.
you have 855933.17 dollars after 8 years later.
you have 970272.17 dollars after 9 years later.
you have 1086326.25 dollars after 10 years later.
you can succeed after 10 years

Process finished with exit code 0

.txt文本输出如下:

文本存储:you have 101500.00 dollars after 1 years later.
文本存储:you have 204522.50 dollars after 2 years later.
文本存储:you have 309090.34 dollars after 3 years later.
文本存储:you have 415226.69 dollars after 4 years later.
文本存储:you have 522955.09 dollars after 5 years later.
文本存储:you have 632299.42 dollars after 6 years later.
文本存储:you have 743283.91 dollars after 7 years later.
文本存储:you have 855933.17 dollars after 8 years later.
文本存储:you have 970272.17 dollars after 9 years later.
文本存储:you have 1086326.25 dollars after 10 years later.
文本存储:you can succeed after 10 years

如果您想了解更多C++/Java/机器学习相关的知识,欢迎扫描下方的二维码,关注“梁公子的备忘录”,每天一篇相关的技术分享,期待与您一起学习,共同进步!

在这里插入图片描述

你可能感兴趣的:(Java)