我希望用户从JOptionPane中选择一个选项。然后我想在if语句中使用该选项。但我不知道该怎么做? 这是我的代码:
import javax.swing.JOptionPane;
public class MortgageCalculator {
public static void main(String[]args)
{
JOptionPane.showMessageDialog(null, "Lets calculate your mortgage",null, JOptionPane.PLAIN_MESSAGE);
double principle = Double.parseDouble(JOptionPane.showInputDialog(null, "What is your loan amount?"));
int years = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the lenght of your loan"));
int n = years * 12;
double rate = Double.parseDouble(JOptionPane.showInputDialog(null, "What is your interest rate?"));
String[] options = new String[] {"Compound", "Simple"};
int response = JOptionPane.showOptionDialog(null, null, "Choose your interest type", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,null, options, options[0]);
//I want to use my selected option here.
if(options ==compound)
{
double payment = (compound(principle,years,rate))/n;
}
else
{
double payment = (simple(principle, years, rate))/n;
}
}
public static double simple(double principle, int n , double interestRate)
{
double interest = principle * n * (interestRate/100);
double totalLoan = principle + interest;
return totalLoan;
}
public static double compound(double principle, int years, double interestRate)
{
int n = years * 12;
double base = (principle* (1+ (interestRate/100)*1/n));
double amount = Math.pow(base, n);
return amount;
}
顺便说一句,这不是一个完整的代码。语法缺失。 谢谢您的帮助。
答案 0 :(得分:0)
当你的按钮被提供给String数组中的对话框时,当选择该按钮时,它是该按钮名称所在的索引值,返回的数组。
复合会从对话框中返回 0 的索引值,简单会返回 1 的索引值>
// Compound (0)
if(response == 0) {
double payment = (compound(principle,years,rate))/n;
}
// Simple (1)
else {
double payment = (simple(principle, years, rate))/n;
}
检查响应变量中的值....不是选项。