0

我想知道这是否是解决第 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);
    }
}
4

2 回答 2

0

我刚刚运行了你的代码,它可以工作。@Dennis Meng acm.program.* 是斯坦福用来促进 Java 教学的图书馆。它使调试更容易,因为控制台窗口弹出并且它是交互式的,而不是使用对初学者来说可能有点吓人的控制台

于 2013-08-30T03:10:27.983 回答
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);
    }
}
于 2014-02-20T05:14:17.433 回答