私は、ユーザーがテキストフィールドからの年数での融資額と貸出期間を入力させてください、このプログラムを作成し、それが5パーセントから始まる各金利のための毎月の総支払額が表示されますテキスト領域では8分の1ずつ増加します。これは愚かだが、数値処理以外の値が入力された場合の例外処理を追加するための例外処理の追加方法がわからないことがあります。たとえば、年数を5にする代わりに、ユーザーが5を入力します。 。前もって感謝します。 パッケージローン。のjava - 例外処理
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.geometry.Pos;
public class loan extends Application {
protected TextField tfLoanAmount = new TextField();
protected TextField tfNumberOfYears = new TextField();
protected TextArea textArea = new TextArea();
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
tfNumberOfYears.setPrefColumnCount(2);
tfLoanAmount.setPrefColumnCount(7);
textArea.setPrefColumnCount(30);
// Create a button
Button btShowTable = new Button("Show Table");
// Create a hbox
HBox paneForControls = new HBox(10);
paneForControls.setAlignment(Pos.CENTER);
paneForControls.getChildren().addAll(new Label("Loan Amount"), tfLoanAmount,
new Label("Number of Years"), tfNumberOfYears, btShowTable);
// Create a scrollPane
ScrollPane scrollPane = new ScrollPane(textArea);
// Create a pane
BorderPane pane = new BorderPane();
pane.setTop(paneForControls);
pane.setCenter(textArea);
// Create and register handler
btShowTable.setOnAction(e -> {
print();
});
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("loans"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
private void print() {
// Create a output string
String output = "";
double monthlyInterestRate; // Monthly interest rate
double monthlyPayment; // Monthly payment
// Add table header
output += "Interest Rate Monthly Payment Total Payment\n";
// Calculate and add table with interest rates to output
for (double i = 5.0; i <= 8; i += 0.125) {
monthlyInterestRate = i/1200;
monthlyPayment = Double.parseDouble(tfLoanAmount.getText()) *
monthlyInterestRate/(1 - 1/Math.pow(1 + monthlyInterestRate,
Double.parseDouble(tfNumberOfYears.getText()) * 12));
output += String.format("%-24.3f%-34.2f%-8.2f\n", i,
monthlyPayment, (monthlyPayment * 12) *
Double.parseDouble(tfNumberOfYears.getText()));
}
textArea.setText(output);
}
public static void main(String[] args) {
launch(args);
}
}
あなたはすでに前に何を試してみましたか?あなたのコードのどこで例外処理が必要ですか?あなたのコードをすべてダンプしても、多くの回答は得られません。 – eestrada
ああ私。これはあなたが簡単にgoogleに投票できる理由です(他人によって)。彼らは意味がありません、ちょうど混乱を制限しようとしています。もし(!(isNumerc(tfLoanAmount.getText()))){)(新しいIllegalArgumentExceptionをスローし;}パブリック静的ブールISNUMERIC(String str)文字 { 戻りstr.matches( " - \\ D +(\\ dは+)?。 ? "); } – MikeJRamsey56