我有一个关于扫描仪输入的问题,它要求计算10年的本金、利息和总额,代码是:
package random;
import java.util.Scanner;
public class Interest {
public static void main(String[] args) {
Scanner input = new Scanner(System. in );
System.out.print("The principle is ");
double principle = input.nextDouble();
System.out.print("The interest rate is ");
double interestRate = input.nextDouble();
for (int i = 1; i <= 10; i++) {
double Interest = principle * interestRate;
double Total = principle + Interest;
principle = Total - Interest;
System.out.println("Year " + i + " principle: " + "$" + Total + " " + "Interest: " + "$" + Interest + " " +
"Total: " + "$" + Total);
}
}
}
但是,当我运行它时,它显示:
The principle is 10000.00
The interest rate is 0.05
Year 1 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 2 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 3 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 4 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 5 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 6 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 7 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 8 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 9 principle: $10500.0 Interest: $500.0 Total: $10500.0
Year 10 principle: $10500.0 Interest: $500.0 Total: $10500.0
我认为问题出在我的循环中,有人可以建议吗?干杯!