因此,我正在進行一項學校任務(痛苦地),並且我已經整理了這個特定的問題,減去了一些細節。該計劃基本上爲15個人計算隨機月度儲蓄,介於100美元和800美元之間,然後打印每月保存的金額,該月賺取的利息以及一年的總收入。這是代碼。我不會打擾張貼我的EmployeeSavings類擴展了通訊錄類,因爲它不是真正的問題相關....用於計算興趣的Java程序
import java.util.Arrays;
public class EmployeeSavings extends AddressBook {
private double accountValue;
private double[] monthlyInterests;
private double[] monthlySavings;
public static final double ANNUAL_INTEREST_RATE = 0.05;
public EmployeeSavings(String firstName, String lastName) {
super(firstName, "", lastName);
}
public EmployeeSavings(String firstName, String lastName, double[] d1, double[] d2) {
super(firstName, "", lastName);
this.monthlySavings = d1; // .length?
this.monthlyInterests = d2; // .length?
}
public double getAccountValue() {
return accountValue;
}
public double[] getMonthlyInterests() {
return monthlyInterests;
}
public double[] getMonthlySavings() {
return monthlySavings;
}
public double[] calculateInterests() {
accountValue = 0;
for (int n = 0; n < monthlyInterests.length; n++) {
accountValue += monthlySavings[n];
monthlyInterests[n] = (accountValue * (ANNUAL_INTEREST_RATE/12));
accountValue += monthlyInterests[n];
}
accountValue = accountValue;
return monthlyInterests;
}
public double[] generateMonthlySavings() {
for (int n = 0; n < monthlySavings.length; n++) {
monthlySavings[n] = (Math.random() * 700 + 100);
}
return monthlySavings;
}
public static String getReport(EmployeeSavings[] arr) {
String report = "";
for (EmployeeSavings calculate : arr) {
calculate.calculateInterests();
report += "<" + calculate.getFirstName() + " " + calculate.getLastName()
+ "> \n\nMonthly savings for 12 months: \n"
+ Arrays.toString(calculate.getMonthlySavings())
+ "\n\nInterest earned for each month: \n"
+ Arrays.toString(calculate.getMonthlyInterests())
+ "\n\nTotal account value after 12 months: $" + calculate.getAccountValue() + "\n\n\n";
}
return report;
}
}
,並且測試類....
public class TestEmployeeSavings {
public static void main(String[] args) {
EmployeeSavings[] employees = new EmployeeSavings[] {
new EmployeeSavings("Elena", "Brandon", new double[12], new double[12]),
new EmployeeSavings("Thomas", "Molson", new double[12], new double[12]),
new EmployeeSavings("Hamilton", "Winn", new double[12], new double[12]),
new EmployeeSavings("Suzie", "Sarandin", new double[12], new double[12]),
new EmployeeSavings("Philip", "Winne", new double[12], new double[12]),
new EmployeeSavings("Alex", "Trebok", new double[12], new double[12]),
new EmployeeSavings("Emma", "Pivoto", new double[12], new double[12]),
new EmployeeSavings("John", "Lenthen", new double[12], new double[12]),
new EmployeeSavings("James", "Lean", new double[12], new double[12]),
new EmployeeSavings("Jane", "Ostin", new double[12], new double[12]),
new EmployeeSavings("Emily", "Car", new double[12], new double[12]),
new EmployeeSavings("Daniel", "Hamshire", new double[12], new double[12]),
new EmployeeSavings("Neda", "Bazdar", new double[12], new double[12]),
new EmployeeSavings("Aaron", "Smith", new double[12], new double[12]),
new EmployeeSavings("Kate", "Hen", new double[12], new double[12]) };
for (EmployeeSavings calculate : employees) {
calculate.generateMonthlySavings();
}
System.out.println(EmployeeSavings.getReport(employees));
}
}
的輸出我得到每個人是這樣的:
每月可節省12個月: [419.33855414289945,685.3556014985876,750.0193923605882,419.55644434159916,689.0894339019552,114.41141555507095,266.1838438533235,136.00655226158167,232.40836179739927 ,577.1312357147676,535.620933915401,451.8150715112862]
所得利息爲每個月: [1.7472439755954143,4.61017249840451,7.754462351983647,9.53492446320691,12.445859289728418,12.974431268248416,14.137590747921632,14.763191343794562,15.793072815216203,18.26359076742447,20.57144295360291,22.539720097206615]
總帳戶價值:$ 5432.072543426794
我想知道如何調整我的儲蓄/利息方法,以便每個金額都打印到小數點後第二位(適當的美元金額),之前有一個$符號。我還注意到,每個儲蓄金額/利息的列表都包含在[]中,這有點令人討厭,我想解決這個問題,但不知道如何解決。任何幫助將不勝感激! :)
P.S. EmployeeSavings類中的所有變量/方法都需要作爲該程序的預期API的一部分,並且他們似乎不願意偏離它們,所以這不是一個真正的選擇。
你爲什麼不先試試google「java如何以美元格式打印」? –