我正在编写一个Java程序,用户在该程序中输入销售额,然后输入代码(1,2或3),然后程序根据销售类型(用户输入的代码)计算佣金)。它适用于1和3,但由于一些奇怪的原因,当我输入2代码时它什么都不做,只是关闭,即使代码几乎与1和3相同。任何帮助将不胜感激。
public class TravelCommission extends Frame
{
static double dollars;
static double answer;
static int empCode;
static DecimalFormat df = new DecimalFormat("#.#");
public static void main(String[] args)
{
//Calling all methods
dollars = getSales(0);
empCode = getCode(0);
answer = getCommission(dollars, empCode);
output(dollars, answer);
finish();
}
private static double getSales(double sales)
{
//Getting sales along with error checking and exception handeling
try
{
sales = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter in sales:", "Sales", JOptionPane.DEFAULT_OPTION));
if(sales <= 0)
{
JOptionPane.showMessageDialog(null, "Please enter a value greater than 0.", "Error", JOptionPane.INFORMATION_MESSAGE);
}
else if(sales == JOptionPane.CANCEL_OPTION)
{
System.exit(0);
}
else
{
}
}
catch (IllegalArgumentException e)
{
JOptionPane.showMessageDialog(null, "Enter in a double value.", "Error!", JOptionPane.ERROR_MESSAGE);
}
return sales;
}
private static int getCode(int code)
{
try
{
code = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the code for the type of sale\n1 = Telephone\n2 = In-store\n3 = Outdoor:", "Sales", JOptionPane.DEFAULT_OPTION));
if(code <= 0 || code > 3)
{
JOptionPane.showMessageDialog(null, "Please enter a 1, 2, or 3.", "Error", JOptionPane.INFORMATION_MESSAGE);
}
else if(code == JOptionPane.CANCEL_OPTION)
{
System.exit(0);
}
else
{
}
}
catch (IllegalArgumentException e)
{
JOptionPane.showMessageDialog(null, "Please enter in a 1, 2, or 3.", "Error!", JOptionPane.ERROR_MESSAGE);
}
return code;
}
private static double getCommission(double dollars, int empCode)
{
//Determing the amount of commission made based off of the type of sale
if(empCode == 1)
{
answer = dollars * 0.10;
}
else if(empCode == 2)
{
answer = dollars * 0.14;
}
else if(empCode == 3)
{
answer = dollars * 0.18;
}
return answer;
}
//Outputing the commission
public static int output(double dollars, double answer)
{
JOptionPane.showMessageDialog(null, "Your total commission on sales of $" + dollars + " is $" + df.format(answer));
return 0;
}
//Closes the program
public static void finish()
{
System.exit(0);
return;
}
}
答案 0 :(得分:0)
else if (sales == JOptionPane.CANCEL_OPTION) {
System.exit(0);
}
在JOptionPane
班,
public static final int CANCEL_OPTION = 2;
因此,值2将使您退出并关闭框架。