2017-04-03 68 views
0

我想計算稅和NI計算器。我設法爲它編寫代碼,但似乎無法使GUI工作。我試圖實現許多不同的例子,但他們似乎給出了錯誤。我只是想保持它的簡單,我甚至試圖消息對話框,並用OK按鈕,一個正常的GUI屏幕Java稅和NI GUI計算器

代碼計算器:(用戶輸入他們的收入,並計算出稅等)

package netincome; 
 

 
import java.io.*; 
 

 

 
public class NetIncome { 
 

 
    int pan; 
 
    String name; 
 
    double taxableincome; 
 
    double tax; 
 
    double taxpermonth; 
 
    double annualNIpayments; 
 
    double NIpermonth; 
 
    double netmonthlyincome; 
 

 
    void input() throws IOException { 
 
     InputStreamReader in = new InputStreamReader(System.in); 
 
     BufferedReader br = new BufferedReader(in); 
 
     System.out.println("Enter taxable income:"); 
 
     taxableincome = Double.parseDouble(br.readLine()); 
 
    } 
 

 
    void computeData() { 
 
     if (taxableincome <= 11000) { 
 
      tax = 0; 
 
      taxpermonth = tax/12; 
 
      annualNIpayments = 0 * 0.00; 
 
      NIpermonth = annualNIpayments/12; 
 
      netmonthlyincome = ((taxableincome - tax - annualNIpayments)/12); 
 
     } else if (taxableincome > 11001 && taxableincome <= 43000) { 
 
      tax = (taxableincome * 0.20); 
 
      taxpermonth = tax/12; 
 
      annualNIpayments = taxableincome * 0.12; 
 
      NIpermonth = annualNIpayments/12.0; 
 
      netmonthlyincome = ((taxableincome - tax - annualNIpayments)/12); 
 
     } else if (taxableincome > 43001 && taxableincome <= 150000) { 
 
      tax = (taxableincome * 0.40); 
 
      taxpermonth = tax/12.0; 
 
      annualNIpayments = taxableincome * 0.02; 
 
      NIpermonth = annualNIpayments/12.0; 
 
      netmonthlyincome = ((taxableincome - tax - annualNIpayments)/12); 
 
     } else if (taxableincome > 150001) { 
 
      tax = (taxableincome * 0.45); 
 
      taxpermonth = tax/12.0; 
 
      annualNIpayments = taxableincome * 0.02; 
 
      NIpermonth = annualNIpayments/12.0; 
 
      netmonthlyincome = ((taxableincome - tax - annualNIpayments)/12); 
 
     } 
 
    } 
 

 
    void displayData() { 
 
      
 
     System.out.println("Taxable Income =" + taxableincome); 
 
     System.out.println("Annual Tax Paid =" + tax); 
 
     System.out.println("Monthly Tax Paid ="+ taxpermonth); 
 
     System.out.println("Annual NI =" + annualNIpayments); 
 
     System.out.println("Monthly NI =" + NIpermonth); 
 
     System.out.println("Net Monthly Income =" + netmonthlyincome); 
 
     
 
    } 
 

 
    public static void main(String args[]) throws IOException { 
 
     NetIncome ob = new NetIncome(); 
 
     ob.input(); 
 
     ob.computeData(); 
 
     ob.displayData(); 
 
    } 
 
}

例如我用這個網站獲得的對話框,但似乎無法使if語句來進行這項工作:Message Dialog Code

其他的人是一個GUI畫面。 在此先感謝。 我知道我的代碼並不好,但我只是一個初學者。

+1

請添加有關你得到了錯誤,什麼樣的結果你期待,因爲這是,你是在下降的細節:「爲什麼不是我的代碼工作?」 SO上的題外話題。 – Berger

+0

關於GUI的具體內容不起作用?從您的所有代碼中都不能立即明白。 – Thunderforge

回答

0

這樣做的一個簡單的方法是:

更換

System.out.println("Enter taxable income:"); 
taxableincome = Double.parseDouble(br.readLine()); 

有:

String input = JOptionPane.showInputDialog(null, "Enter taxable income:"); 
taxableincome = Double.parseDouble(input); 

然後輸出(我會讓你完成其餘部分關閉)

替換:

System.out.println("Taxable Income =" + taxableincome); 

有:

JOptionPane.showMessageDialog(null, "Taxable Income = "+ taxableincome); 
+0

謝謝你。我早些時候嘗試過,它不會工作。然後我意識到我正在Javafx上嘗試它。我切換到一個普通的Java項目,它的工作原理 – Veej