我已经完成了我需要计算一年大学十年学费的部分,但是在4年计算方面遇到了麻烦。
作业:编写Java程序以获得每年的学费。十年内输出学费到一年制大学和十年四年制大学学费。您可以假设年利率为5%。
提示:使用While循环结构
输入/输出样本:
Enter the tuition cost: 10000
Tuition in ten years is 16288.946267774418
The four-year tuition in ten years is 70207.39453239122
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);
}
}
Current output:
Enter the tuition cost:
10000
Tuition in ten years is: 16288.94626777442
The four-year tution in ten years is: 16289.996267774419
发布于 2019-04-08 10:40:16
十年后四年的学费将是:
fourYearTuiton = year10Tuition + year11Tuition + year12Tuition + year13Tuition
所以,你可以像这样计算:
fourYearTuiton = 10000 * (1.05^10) + 10000 * (1.05^11) + 10000 * (1.05^12) + 10000 * (1.05^13)
在Java中:
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);
}
}
https://stackoverflow.com/questions/-100006549
复制相似问题