JAVA黑皮书课后练习题15章15.5

*15.5 (Create an investment-value calculator) Write a program that calculates the future value of an investment at a given interest rate for a specified number of years. The formula for the calculation is:futureValue = investmentAmount * (1 + monthlyInterestRate)^(years*12)

futureValue = investmentAmount * (1 + monthlyInterestRate)^(years*12)

Use text fields for the investment amount, number of years, and annual interest rate. Display the future amount in a text field when the user clicks the Calculate button, as shown in Figure 15.25b

 

 

package section_15;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class _05 extends Application {
	public void start(Stage primaryStage) {
		GridPane pane = new GridPane();

		Label labelInvestmentAmount = new Label("Investment Amount:");
		Label labelNumberOfYears = new Label("Number of Years:");
		Label labelAnnualInterestRate = new Label("Annual Interest Rate:");
		Label labelFutureValue = new Label("Future Value:");

		TextField tfInvestmentAmount = new TextField();
		tfInvestmentAmount.setAlignment(Pos.BASELINE_RIGHT);
		TextField tfNumberOfYears = new TextField();
		tfNumberOfYears.setAlignment(Pos.BASELINE_RIGHT);
		TextField tfAnnualInterestRate = new TextField();
		tfAnnualInterestRate.setAlignment(Pos.BASELINE_RIGHT);
		TextField tfFutureValue = new TextField();
		tfFutureValue.setAlignment(Pos.BASELINE_RIGHT);

		Button btCalculate = new Button("Calculate");
		GridPane.setHalignment(btCalculate, HPos.RIGHT);
		btCalculate.setOnAction(event -> {
			double investmentAmount = Double.parseDouble(tfInvestmentAmount.getText());
			double numberOfYears = Double.parseDouble(tfNumberOfYears.getText());
			double annualInterestRate = Double.parseDouble(tfAnnualInterestRate.getText());
			double futureValue = investmentAmount * Math.pow(1 + (annualInterestRate / 1200), numberOfYears * 12);

			futureValue = Math.round(futureValue * 100) / 100.0;
			tfFutureValue.setText("$" + futureValue);
		});

		pane.add(labelInvestmentAmount, 0, 0);
		pane.add(labelNumberOfYears, 0, 1);
		pane.add(labelAnnualInterestRate, 0, 2);
		pane.add(labelFutureValue, 0, 3);
		pane.add(tfInvestmentAmount, 1, 0);
		pane.add(tfNumberOfYears, 1, 1);
		pane.add(tfAnnualInterestRate, 1, 2);
		pane.add(tfFutureValue, 1, 3);
		pane.add(btCalculate, 1, 4);
		pane.setHgap(10);
		pane.setVgap(5);
		pane.setPadding(new Insets(20));
		Scene scene = new Scene(pane);

		primaryStage.setTitle("_15_05");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		Application.launch(args);
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java黑皮课后习题10.11要求我们编写一个程序,该程序在一个二维数组中查找是否存在一个指定的值。如果存在,则返回该值的位置(行和列),否则返回(-1, -1)。 首先,我们需要创建一个二维数组,并初始化它。然后,通过遍历数组的每个元素来查找指定值。当找到指定值时,记录其位置并返回。 以下是解题思路: 1. 创建一个名为findValue的方法,参数为一个二维整数数组和一个整数值。 2. 在方法内,使用两个嵌套的for循环遍历数组的每个元素,外层循环控制行,内层循环控制列。 3. 在每次循环中,检查当前元素是否等于指定值。如果是,返回当前位置(row, column)。 4. 如果遍历完整个数组都没有找到指定值,返回位置(-1, -1)。 以下是代码示例: ```java public class Main { public static void main(String[] args) { // 创建二维数组 int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int target = 5; // 指定值 // 调用findValue方法查找指定值的位置 int[] position = findValue(array, target); // 输出结果 if (position[0] == -1 && position[1] == -1) { System.out.println("指定值不存在!"); } else { System.out.println("找到指定值,位置为(" + position[0] + ", " + position[1] + ")"); } } public static int[] findValue(int[][] array, int target) { for (int row = 0; row < array.length; row++) { for (int column = 0; column < array[row].length; column++) { if (array[row][column] == target) { return new int[]{row, column}; } } } return new int[]{-1, -1}; } } ``` 以上代码会输出"找到指定值,位置为(1, 1)",即指定值5在二维数组的第2行第2列。如果指定值不在数组中,则输出"指定值不存在!"。 希望这个回答对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值