有任何方法可以更好地构建Java中的许多if if语句

时间:2015-03-17 04:17:56

标签: java if-statement

以下是我对税务计划的代码。是否有更好的方法来构建if else语句,并且还有更好的方法来编写程序。我事先道歉没有任何意见。

    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");

    }

}

1 个答案:

答案 0 :(得分:0)

在实际系统中,所有这些金额,费率和阈值肯定都在数据库表中,处理付款只需要应用一个简单的算术公式,该公式涉及从适用的行中获取的值。考虑按照这些方针重组整个事情。