1
javafxを使用して、毎月の利率が.125(5%から8%)増加することに基づいて月額および合計支払い額を計算するためのローン金額と年数を求める枠を作成しています。javafxを使って複数行のテキストをテキスト領域に追加するには?
これを最初のペインの下にテーブルの形で印刷したいと思います。しかし、テキストエリア変数に複数行のテキストを追加する方法を理解しようとしています。
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
public class LoanTable extends Application {
// create TextField and button variables
private TextField tfAmount = new TextField();
private TextField tfYears = new TextField();
private Button btCalc = new Button("Show Table");
private TextArea taResult = new TextArea();
@Override
public void start(Stage primaryStage) {
// create FlowPane and set properties
FlowPane pane = new FlowPane();
pane.setHgap(10);
tfAmount.setPrefColumnCount(3);
tfYears.setPrefColumnCount(3);
pane.getChildren().addAll(new Label("Loan Amount"), tfAmount, new Label("Number of Years"), tfYears, btCalc);
// create ScrollPane and set properties
ScrollPane scrollPane = new ScrollPane(taResult);
taResult.setEditable(false);
// create BorderPane and add FlowPane and ScrollPane
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(scrollPane);
// create and register a handler
btCalc.setOnAction(e -> calcTable());
// create a scene and place it in the stage
Scene scene = new Scene(borderPane);
primaryStage.setTitle("Loan-Table");
primaryStage.setScene(scene);
primaryStage.show();
}
// create a method to calculate monthly payment
public double calcMonthly(double interestRate) {
double loanAmount = Double.parseDouble(tfAmount.getText());
int years = Integer.parseInt(tfYears.getText());
double monthlyInterestRate = interestRate/1200;
double monthlyPayment = loanAmount * monthlyInterestRate/(1 - 1/Math.pow(1 + monthlyInterestRate, years * 12));
return monthlyPayment;
}
// create a method to calculate total payment
public double calcTotal(double interestRate, double monthlyPayment) {
int years = Integer.parseInt(tfYears.getText());
double totalPayment = monthlyPayment * years * 12;
return totalPayment;
}
private void calcTable() {
// get values from text fields
double interestRate = 5;
// display table header
taResult.setText(String.format("%13s%20s%18s\n", "Interest Rate", "Monthly Payment", "Total Payment"));
// print rest of table
for (int i = 1; i < 26; interestRate+=.125, i++) {
double x = calcMonthly(interestRate);
taResult.setText(String.format("%5.3f%19.2f%22.2f\n", interestRate, calcMonthly(interestRate), calcTotal(interestRate, x)));
}
}
public static void main(String args[]) {
Application.launch(args);
}
}
問題がループ「テーブルの//印刷休息」で来る:
は、ここに私のコードです。 taResult変数に複数行のテキストを追加するにはどうすればよいですか?このプログラムを実行すると、テーブルの最後の行が表示されます。 (数年間)(ローン額のために)入力10000と5と出力は次のようになります。
8.000 202.76 12165.84
しかし、私の所望の出力は次のようになります。
Interest Rate Monthly Payment Total Payment
5.000 188.71 11322.74
5.125 189.29 11357.13
5.250 189.86 11391.59
5.375 190.44 11426.11
5.500 191.01 11460.70
5.625 191.59 11495.35
5.750 192.17 11530.06
5.875 192.75 11564.84
6.000 193.33 11599.68
6.125 193.91 11634.59
6.250 194.49 11669.56
6.375 195.08 11704.59
6.500 195.66 11739.69
6.625 196.25 11774.85
6.750 196.83 11810.08
6.875 197.42 11845.37
7.000 198.01 11880.72
7.125 198.60 11916.14
7.250 199.19 11951.62
7.375 199.79 11987.16
7.500 200.38 12022.77
7.625 200.97 12058.44
7.750 201.57 12094.18
7.875 202.17 12129.97
8.000 202.76 12165.84
私は問題が明らかにしたいと考えています。