简体   繁体   English

用任何方法更好地构造Java中的if语句

[英]Any way to better structure many if else statements in Java

Here is the code I have for a tax program.Is there a better way to structure the if else statements and also is there a better way to write the program. 这是我为税务程序编写的代码,是否有更好的方法来构造if else语句,也有更好的方法来编写程序。 I apologize before hand for not having any comments. 对于没有任何评论,我深表歉意。

    package jOptionPane;

    import javax.swing.JOptionPane;

    public class Main {

    public static void main(String[] args)
    {
        double paycheck = Double.parseDouble(JOptionPane.showInputDialog("How much was your paycheck?"));
        String[] options = new String[] {"Yes", "No"};
        int response = JOptionPane.showOptionDialog(null, "Are you married?", "Title",
            JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, options, options[0]);
        double deductions = Integer.parseInt(JOptionPane.showInputDialog("How many deductions did you claim?"));
        String[] payOften = new String[] {"Weekly", "Biweekly"};
        int variability = JOptionPane.showOptionDialog(null, "How often do you get paid?", "Title",
            JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, payOften, payOften[0]);
        double SS = 0;

        if (response == 0)
        {
            if (variability == 0)
            {
                 if(paycheck <= 490)
                 {
                     SS = (paycheck - (deductions*73.08)) * .1;
                 }

                 else if (paycheck <= 1515)
                 {
                     SS = (paycheck - (deductions*73.08)) * .15 + 33.4;
                 }
                 else
                 {
                     System.out.println("I am too lazy to compute");
                 }
            }
            else 
            {
                if(paycheck <= 981)
                {
                    SS = (paycheck - (deductions * 146.15)) * .1;
                }
                else if (paycheck <= 3031)
                {
                    SS = (paycheck - (deductions * 146.15)) * .15 + 66.9;
                }
                else 
                {
                    System.out.println("I am too lazy to compute");
                }
            }
        }
        else
        {
            if (variability == 0)
            {
                if(paycheck <= 209)
                {
                    SS = (paycheck - (deductions * 73.08)) * .1;
                }
                else if (paycheck <= 721)
                {
                    SS = (paycheck -(deductions * 73.08)) * .15 + 16.8;
                }
                else
                {
                    System.out.println("I am too lazy to compute.");
                }
            }
            else
            {
                if(paycheck <= 417)
                {
                    SS = (paycheck - (deductions * 146.15)) * .1;
                }
                else if (paycheck <= 1442)
                {
                    SS = (paycheck - (deductions * 146.15)) * .15 + 33.4;
                }
                else 
                {
                    System.out.println("I am too lzy to compute.");
                }
            }
        }
        System.out.println("You owe " + SS + " for Social Security");

    }

}

In a real system all these amounts and rates and thresholds would certainly be in a database table, and processing a payment would just be a matter of applying a simple arithmetic formula involving values taken from the applicable row(s). 在实际系统中,所有这些金额,费率和阈值肯定都在数据库表中,并且处理付款将仅是应用一个简单的算术公式,其中涉及从适用行中获取的值。 Have a think about restructuring the whole thing along those lines. 考虑按照这些思路重组整个事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM