如何用JAVA编写:利率固定,在经过多年以后使利息和本金是存款的两倍

来源:百度知道 编辑:UC知道 时间:2024/05/23 09:48:30
Write an interactive Java program to calculate how many years it takes to double your fix-amount of savings in a bank. Suppose the interest rate is fixed.
For example, if user saves 1000 dollars into bank, so at bank interest rate 7%, it may take at least 11 years to have the balance become 2000 dollars. Name your class as DoubleSaving.java, write a method with signature as follows:
public int calculateYears( double savingAmount, double interestRate) ;
Call the method calculateYears in your main method.

When you program is running, it should behave like this:

C:\MyJava>java DoubleSaving
Your saving balance?
1000
Interest rate?
0.07
It takes at least 11 years to double your savings.

import java.io.*;
public class DoubleSaving{
public static void main(String [] args)throws Exception{
System.out.println ("Your saving balance? ");
BufferedReader br0 = new BufferedReader(new InputStreamReader(System.in));
float balance =Float.parseFloat(br0.readLine());
System.out.println ("Interest rate?");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
float rate = Float.parseFloat(br1.readLine());
float sum=0;
int year=0;
float newbalance=2*balance;
while(sum<newbalance){
balance=balance+balance*rate;
sum=balance;
year++;
}
System.out.println ("It takes at least "+year+" years to double your savings");
}
}

补充:
用javax.swing*.包中
String s=JOptionPane.showInputDialog("请输入你想要的数字");//传如您想要的数字信息,被转化成字符串.
int a=Integer.ParseInt(s).//把字符串转换成int形式传给a,