标签:java
*4.21(计算不同利率下的贷款)编写程序,让用户输入贷款总额及以年为单位的贷款期限,以1/8为递增量,显示从5%到8%的利率下每月支付额和总偿还额。假设输入贷款总量为10 000,还贷期限为5年,所显示的输出如下:
贷款总额:to 000
年数:5
利率月支付额总偿还额
5%188 .71 11322.74
5 .125%189.28 11357.13
5 .25%189.85 11391.59
...
//Exercise3_26.java: displays the monthly and total payments //for each interest rate starting from 5% to 8%, //with an incremental of 1/8. import javax.swing.JOptionPane; public class Exercise3_26 { // Main method public static void main(String[] args) { int numOfYears; double loanAmount; // Enter number of years String numOfYearsString = JOptionPane.showInputDialog(null, "Enter number of years as an integer, \nfor example 5:", "Exercise3_26 Input", JOptionPane.QUESTION_MESSAGE); // Convert string to int numOfYears = Integer.parseInt(numOfYearsString); // Enter loan amount String loanString = JOptionPane.showInputDialog(null, "Enter loan amount, for example 120000.95:", "Exercise3_26 Input", JOptionPane.QUESTION_MESSAGE); // Convert string to double loanAmount = Double.parseDouble(loanString); // Display the header System.out.print("Interest Rate"); System.out.print("\t" + "Monthly Payment"); System.out.println("\t\t" + "Total Payment"); double monthlyInterestRate; double monthlyPayment; double totalPayment; for (double annualInterestRate = 5.0; annualInterestRate <= 8.0; annualInterestRate += 1.0 / 8) { // Obtain monthly interest rate monthlyInterestRate = annualInterestRate / 1200; // Compute mortgage monthlyPayment = loanAmount*monthlyInterestRate/ (1 - (Math.pow(1 / (1 + monthlyInterestRate), numOfYears * 12))); totalPayment = monthlyPayment * numOfYears * 12; // Display results System.out.print(annualInterestRate + "%"); System.out.print("\t\t" + (int)(monthlyPayment * 100) / 100.0); System.out.println("\t\t\t" + (int)(totalPayment * 100) / 100.0); } } }
**4.22(显示分期还贷时间表)对于给定的贷款额、月支付额包括偿还本金及利息。月利息是通过月利
率乘以余额(剩余本金)计算出来的。因此,每月偿还的本金等于月支付额减去月利息。编写一个程序,让用户输入贷款总额、贷款年数及利率,而后显示分期还贷时间表。假设输入的贷款总额为10 000,还贷期限为1年,年利率为7%,显示如下的表格:
贷款总额:10 000
年数:1
年利率:7%
月支付额:865.26
总偿还额:
10383.21
月份 利息 本金 余额
1 58.33 806.93 9193.07
2 53.62 811.64 8381.43
...
1 1 10.0 855.26 860.27
1 2 5.01 860.25 0.01
注:最后一次偿还后,余额可能不为0。如果是这样的话,最后一个月支付额应当是正常的月支付额加上最后的余额。
提示编写一个循环来打印该表。由于每个月的还贷额都是相同的,因此,应当在循环之前将其计算出来。开始时,余额即为贷款总额。在循环的每次迭代中,计算利息及本金,并更新余额。这个循环可以这样:
for (i =1;i <= numberOfYears * 12;i++) {
interest = monthlyInterestRate * balance;
principal = monthlyPayment - interest;
balance = balance - principal;
System.out.println(i + "\t\t" +interest
+ "\t\t" + principal +"\t\t" + balance);
}
import javax.swing.JOptionPane; public class Exercise3_27 { public static void main(String[] args) { int numOfYears; double loanAmount; // Enter yearly interest rate String annualIntrestRateString = JOptionPane.showInputDialog( "Enter yearly interest rate, for example 8.25:"); // Convert string to double double annualInterestRate = Double.parseDouble(annualIntrestRateString); // Obtain monthly interest rate double monthlyInterestRate = annualInterestRate/1200; // Enter number of years String numOfYearsString = JOptionPane.showInputDialog( "Enter number of years as an integer, \nfor example 5:"); // Convert string to int numOfYears = Integer.parseInt(numOfYearsString); // Enter loan amount String loanString = JOptionPane.showInputDialog( "Enter loan amount, for example 120000.95:"); // Convert string to double loanAmount = Double.parseDouble(loanString); // Compute mortgage double monthlyPayment = loanAmount*monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numOfYears * 12))); double balance = loanAmount; double interest; double principal; System.out.println("Loan Amount: " + loanAmount); System.out.println("Number of Years: " + numOfYears); System.out.println("Interest Rate: " + annualInterestRate + "%"); System.out.println(); System.out.println("Monthly Payment: " + (int)(monthlyPayment * 100) / 100.0 ); System.out.println("Total Payment: " + (int)(monthlyPayment * 12 * numOfYears * 100) / 100.0 + "\n" ); // Display the header System.out.println("Payment#\tInterest\tPrincipal\tBalance"); int i; for (i = 1; i <= numOfYears * 12; i++) { interest = (int)(monthlyInterestRate * balance * 100) / 100.0; principal = (int)((monthlyPayment - interest) * 100) / 100.0; balance = (int)((balance - principal) * 100) / 100.0; System.out.println(i + "\t\t" + interest + "\t\t" + principal + "\t\t" + balance); } } }
近似计算π
π = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 -...- 1/(2i - 1) + 1/(2i + 1)
public class Exercise3_30 { public static void main(String[] args) { double pi = 0; double term; int sign = 1; for (int i = 1; i <= 100000; i++) { term = sign * 4.0 / (2 * i - 1); pi += term; sign = -1 * sign; if (i == 10000 || i == 20000 || i == 30000 || i == 40000 || i == 50000 || i == 60000 || i == 70000 || i == 80000 || i == 90000 || i == 100000) System.out.println("The PI is " + pi + " for i = " + i); } } }
近似计算e
e = 1 + 1/1! + 1/2! + 1/3!+ ...+ 1/i!
新的一项item由前面一个item除以i得到
public class Exercise3_31 { public static void main(String[] args) { double e = 1; double item = 1; for (int i = 1; i <= 100000; i++) { item = item / i; e += item; if (i == 10000 || i == 20000 || i == 30000 || i == 40000 || i == 50000 || i == 60000 || i == 70000 || i == 80000 || i == 90000 || i == 100000) System.out.println("The e is " + e + " for i = " + i); } } }
Java语言程序设计基础篇 循环(四)练习,布布扣,bubuko.com
标签:java
原文地址:http://coolview.blog.51cto.com/7138551/1408129