dogrowth.java
来自「Java 入门书的源码」· Java 代码 · 共 40 行
JAVA
40 行
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.
/* Enhances Example 4.5 to ask if the user wants to
* repeat the calculation
*
* Input: Rate, a double value giving the yearly percent interest rate
* Balance, a double value giving the amount deposited
* Years, an int value giving the time period for the deposit
* Repeat, 1 to repeat, 0 to quit
*
* Output: Amount, the total amount in the account at the end of the
* time period.
*/
import iopack.Io;
public class DoGrowth {
public static void main(String [] args) {
double rate; // yearly percent interest rate
double balance; // amount on deposit
int years; // time period of the deposit
double interest; // interest earned in current year
int repeat; // 1 to repeat, 0 to quit
do {
rate = Io.readDouble("Enter the percent interest rate");
balance = Io.readDouble("Enter the initial balance");
years = Io.readInt("Enter the time period for the deposit");
for (int i = 1; i <= years; i++) {
interest = balance * rate/100;
balance += interest;
}
System.out.print ("The balance after " + years + " years is ");
Io.println$(balance);
repeat = Io.readInt("Enter 1 to repeat, 0 to quit");
} while (repeat == 1);
Io.readString("Press any key to exit"); // Added for IDE use
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?