第二章第二十题(金融应用:计算利息)(Financial application: calculate interest)

*2.20(金融应用:计算利息)如果知道余额和年利率百分比,就可以使用下面的公式计算下个月的利息:

利息 = 余额 * (年利率百分比 / 1200)

编写程序,读取余额和年利率百分比,打印下个月的利息。下面是一个运行示例:

Enter balance and interest rate (e.g.,3 for 3%):1000 3.5

The interest is 2.91667 

 

*2.20(Financial application: calculate interest) If you know the balance and the annual percentage interest rate, you can compute the interest on the next monthly payment using the following formula:

interest = balance * (annualInterestRate/1200)

Write a program that reads the balance and the annual percentage interest rate and displays the interest for the next month. Here is a sample run:

Enter balance and interest rate (e.g.,3 for 3%):1000 3.5

The interest is 2.91667 

 

下面是参考答案代码:

import java.util.*;


public class CalculateInterestQuestion20 {
	public static void main(String[] args) {
		double Interest,Balance,AnnualInterestRate;
		
		System.out.print("Enter balance and interest rate "
						+ "(e.g., 3 for 3%) : ");
		Scanner BalanRateInput = new Scanner(System.in);
		Balance = BalanRateInput.nextDouble();
		AnnualInterestRate = BalanRateInput.nextDouble();
		
		Interest = Balance * (AnnualInterestRate / 1200);
		System.out.println("The interest is " + Interest);
		
		BalanRateInput.close();
	}
}

运行效果:

 

注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)

你可能感兴趣的:(#,第二章课后习题答案)