2017-04-19 30 views
-2

我正在編寫一個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; 
} 

}

+1

你可能想看看'JOptionPane.CANCEL_OPTION'是什麼值。 –

+2

[你的步調試器告訴你什麼?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –

回答

0
else if (sales == JOptionPane.CANCEL_OPTION) { 
System.exit(0); 
} 

JOptionPane類,

public static final int CANCEL_OPTION = 2; 

因此,值2將使您退出和框架閉合。

+1

Ahhh好。我沒有想到這一點。謝謝! –

+0

你使用IDE嗎?你可以點擊'ctrl' +鼠標點擊來檢查。 – Tony

+0

這也意味着您嘗試檢查用戶是否按下取消不起作用。如果我按Cancel,'showInputDialog()'返回'null'和'Integer.parseInt]'拋出一個你捕獲的'NumberFormatException'(因爲它是'IllegalArgumentException'的子類),並且你的程序繼續。 –