我想知道这是否是解决第3章练习2的最佳方法。
练习2:编写一个读取两个数字的程序:帐户余额和年度 利率以百分比表示。然后你的程序应该显示 一年后的新余额。没有存款或取款 - 只是 利息支付。您的程序应该能够重新生成以下内容 样品运行:
利息计算程序
输入起始余额:6000
输入年利率:4.25
一年后的余额= 6255.0
可能的解决方案:
/* Interest.java
* -------------------------------
* Program reads two numbers and
* adds the interest rate.
*/
import acm.program.*;
public class Interest extends ConsoleProgram{
public void run(){
int sBalance = readInt("Enter Balance: ");
Double iRate = readDouble("Enter interest rate: ");
Double yBalance = sBalance + (sBalance * (iRate / 100));
println("The balance after one year = " + yBalance);
}
}
答案 0 :(得分:0)
我刚刚运行了你的代码,它确实有效。 @Dennis Meng acm.program。*是斯坦福用来方便java教学的图书馆。它使调试变得更容易,因为控制台窗口弹出并且它是交互式的,而不是使用可能对初学者来说有点可怕的控制台
答案 1 :(得分:0)
import acm.program.*;
public class CHP3_Exercise2And3 extends ConsoleProgram {
public void run() {
println("Interest calculation program");
double Starting_Balance = readDouble("Enter starting balance: ");
double Annual_Interest_Rate = readDouble("Enter annual interest rate: ");
double Operation_Year1 = ((Starting_Balance * Annual_Interest_Rate)/100)+Starting_Balance;
double Operation_Year2 = ((Operation_Year1 * Annual_Interest_Rate)/100)+Operation_Year1;
println("Balance after one year = " + Operation_Year1);
println("Balance after two years = " + Operation_Year2);
}
}