如何在 if 语句中使用用户从 JOptionPane 选择的选项?
how to use option selected by user from JOptionPane in if statement?
我希望用户select 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;
}
顺便说一句,这不是完整的代码。缺少语法。
谢谢您的帮助。
当您的按钮以字符串数组的形式提供给对话框时,当该按钮被选中时,它就是该按钮名称在数组中所在位置的索引值,即 returned。
Compound 将 return 来自对话框的索引值 0 和 Simple 将 return 的索引值为 1.
// Compound (0)
if(response == 0) {
double payment = (compound(principle,years,rate))/n;
}
// Simple (1)
else {
double payment = (simple(principle, years, rate))/n;
}
检查 response 变量中的值....不是 options。
我希望用户select 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;
}
顺便说一句,这不是完整的代码。缺少语法。 谢谢您的帮助。
当您的按钮以字符串数组的形式提供给对话框时,当该按钮被选中时,它就是该按钮名称在数组中所在位置的索引值,即 returned。
Compound 将 return 来自对话框的索引值 0 和 Simple 将 return 的索引值为 1.
// Compound (0)
if(response == 0) {
double payment = (compound(principle,years,rate))/n;
}
// Simple (1)
else {
double payment = (simple(principle, years, rate))/n;
}
检查 response 变量中的值....不是 options。