Javaプログラミング初心者です。将来の投資価値を計算するプログラムの割り当てがあります。私が既に書いたプログラムは動作しますが、インストラクターがユーザーの入力を表示するように求めており、テキストまたはオンラインでこのトピックに関する情報を見つけることができません。インストラクターにメールを送信しましたが、結果はありません。以下にプログラムを示します。また、小数点以下を四捨五入する方法もわかりません。助けてくれる人に感謝します。
import javax.swing.JOptionPane;
/* This program will calculate the future investment value after entering the
* investment amount, annual interest rate and the number of years for the investment*/
public class FutureInvestment {
public static void main(String[] args){
//Enter annual interest rate
String annualInterestRateString = JOptionPane.showInputDialog(
"Enter annual interest rate, for example, 8.25:");
//Convert string to double
double annualInterestRate = Double.parseDouble(annualInterestRateString);
//Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
//Enter number of years
String numberOfYearsString = JOptionPane.showInputDialog(
"Enter number of years as an integer, for example, 5:");
//Convert string to integer
int numberOfYears = Integer.parseInt(numberOfYearsString);
//Enter investment amount
String investmentAmountString = JOptionPane.showInputDialog(
"Enter investment amount, for example, 120000.95:");
//Convert string to double
double investmentAmount = Double.parseDouble(investmentAmountString);
//Calculate future investment amount
double futureInvestmentAmount = investmentAmount * (Math.pow(1 + monthlyInterestRate,
numberOfYears * 12));
//Format to keep 2 digits after the decimal point
futureInvestmentAmount = (int)(futureInvestmentAmount * 100) / 100.0;
//Display results
String output = "The Future Investment Value is $" + futureInvestmentAmount;
JOptionPane.showMessageDialog(null, output);
}
}