我目前正在研究一个利息计算器,我正在尝试将从“i=0”到“i-1”的方法 interestEarned 返回的所有值获取到 TextArea 中。每次我尝试获取“i”的单个值时,它都不会显示我指定的“i”处的值
我只是希望能够提取从“i=0”到“i-1”的一系列值。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JTextField;
public class Lab7Control extends Lab7View implements ActionListener {
private static final long serialVersionUID = 1L;
protected final boolean verbose = true;
public final int maxAge = 65;
DecimalFormat df = new DecimalFormat("###,###,###.##");
public static void main(String args[]) {
new Lab7Control();
}
public Lab7Control() {
calcB.addActionListener(this);
clearB.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
String whichwidget = e.getActionCommand();
if (whichwidget.equals("Calculate")) {
displayResultPanel();
}
if (whichwidget.equals("Clear")) {
procReset();
}
}
public void procReset() {
if (popupFrame != null) {
popupFrame.dispose();
}
checkGroup.clearSelection();
buttonGroup.clearSelection();
interestOptionList.clearSelection();
yearStart.setText("");
investmentField.setText("");
}
public void displayResultPanel() {
validateInteger(yearStart);
validateDouble(investmentField);
validateSelectionList();
validateSelectionCheck();
if (validateDouble(investmentField) == true && validateInteger(yearStart) == true
&& validateSelectionList() == true && validateSelectionCheck() == true) {
if (earlyBox.isSelected()) {
displayResult();
resultArea.setText(interestEarned());
} else {
displayResult();
resultArea.setText("Total Value of your Accout: " + interestEarnedat65());
}
}
}
public boolean validateInteger(JTextField yearStart) {
try {
int d = Integer.parseInt(yearStart.getText());
if (d > maxAge)
throw new NumberFormatException();
if (verbose)
System.out.println("validated Integer " + d);
return true;
} catch (NumberFormatException e) {
if (verbose)
System.out.println("invalid Integer " + yearStart.getText() + " please retype age less than 65 ");
yearStart.setText("invalid retry");
return false;
}
}
public boolean validateDouble(JTextField investmentField) {
try {
double d = Double.parseDouble(investmentField.getText());
if (verbose)
System.out.println("validated Double " + d);
return true;
} catch (NumberFormatException e) {
if (verbose)
System.out.println("invalid double " + investmentField.getText() + " please retype ");
investmentField.setText("invalid retry");
return false;
}
}
public boolean validateSelectionList() {
if (interestOptionList.getSelectedValue() != null) {
return true;
} else {
System.out.println("Please make a selection");
return false;
}
}
public boolean validateSelectionCheck() {
if (checkGroup.getSelection() != null) {
return true;
} else {
System.out.println("Select when you want to withdraw your money");
return false;
}
}
public double doubletoDecimal() {
double d = Double.parseDouble(interestOptionList.getSelectedValue()) / 100;
return d;
}
public String interestEarnedat65() {
double totalAmount = 0;
double yearlyInterestPaid = 0;
double newAmount = 0;
double q = Double.parseDouble(investmentField.getText());
int h = Integer.parseInt(yearStart.getText());
int ageDifferenceforRetirement = 65 - h;
for (int i = 0; i <= ageDifferenceforRetirement; i++) {
yearlyInterestPaid = (q * doubletoDecimal());
newAmount = (yearlyInterestPaid + totalAmount);
totalAmount = (newAmount * doubletoDecimal()) + newAmount;
System.out.println(i + " " + totalAmount);
}
return df.format(totalAmount);
}
public String interestEarned() {
double totalAmount = 0;
double yearlyInterestPaid = 0;
double newAmount = 0;
int i;
double q = Double.parseDouble(investmentField.getText());
int h = Integer.parseInt(yearStart.getText());
int ageDiff = 65 - h;
for ( i = 0; i <= ageDiff - 1; i++) {
yearlyInterestPaid = (q * doubletoDecimal());
newAmount = (yearlyInterestPaid + totalAmount);
totalAmount = (newAmount * doubletoDecimal()) + newAmount;
System.out.println(i + " " + totalAmount);
}
double[] data = new double[i];
return String.valueOf(data[i-1]);
}
}