首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不知道如何创建一个java程序来计算四年的学费

不知道如何创建一个java程序来计算四年的学费
EN

Stack Overflow用户
提问于 2019-04-08 00:46:52
回答 1查看 0关注 0票数 0

我已经完成了我需要计算一年大学十年学费的部分,但是在4年计算方面遇到了麻烦。

作业:编写Java程序以获得每年的学费。十年内输出学费到一年制大学和十年四年制大学学费。您可以假设年利率为5%。

提示:使用While循环结构

输入/输出样本:

代码语言:javascript
复制
Enter the tuition cost: 10000
Tuition in ten years is 16288.946267774418
The four-year tuition in ten years is 70207.39453239122
代码语言:javascript
复制
import java.util.Scanner;
import static java.lang.Math.pow;

public class interest {

    public static void main(String args[]) {
        Scanner input = new Scanner (System.in);
        double amount = 0;
        double rate = (1+0.05);
        double time = 10;
        double fourYear = 0;
        System.out.println("Enter the tuition cost: ");
        double principle = input.nextDouble();
        int i=0;
        while(i<time) {
            amount= principle * Math.pow(rate, 10);
            i++;
            break;
        }
        System.out.println("Tuition in ten years is:"+amount);
        while(i<=3) { 
            fourYear= amount+ rate;
            i++;
        }
        System.out.println("The four-year tution in ten years is: "  +fourYear);
    }
}

代码语言:javascript
复制
Current output: 
Enter the tuition cost: 
10000
Tuition in ten years is: 16288.94626777442
The four-year tution in ten years is: 16289.996267774419
EN

回答 1

Stack Overflow用户

发布于 2019-04-08 10:40:16

十年后四年的学费将是:

代码语言:javascript
复制
fourYearTuiton = year10Tuition + year11Tuition + year12Tuition + year13Tuition

所以,你可以像这样计算:

代码语言:javascript
复制
fourYearTuiton = 10000 * (1.05^10) + 10000 * (1.05^11) + 10000 * (1.05^12) + 10000 * (1.05^13)

在Java中:

代码语言:javascript
复制
import java.util.Scanner;
import static java.lang.Math.pow;

public class interest {

    public static void main(String args[]) {
        Scanner input = new Scanner (System.in);
        double tuitonInTenYears = 0;
        double rate = (1+0.05);
        double time = 10;
        double fourYear = 0;
        System.out.println("Enter the tuition cost: ");
        double principle = input.nextDouble();
        int i=0;
        tuitonInTenYears = principle * Math.pow(rate, 10);
        System.out.println("Tuition in ten years is:"+tuitonInTenYears);
        while(i<=3) { 
            fourYear= fourYear + principle * Math.pow(rate, 10 + i);
            i++;
        }
        System.out.println("The four-year tution in ten years is: "  +fourYear);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档