我正在为 CS 课程制定税收计划。我在java中使用neatbeans和编程。我是编程新手。我的代码似乎正在工作,但是当我从我的 JComboBox 中选择“单一”以外的选择时,数学并没有改变它应该的方式。它继续使用“单一”选择中的数学。我想知道如何解决这个问题。另外,有一个随机的 2000 美元似乎在数学中浮动,所以我的计算有问题,但对于我的生活,我无法弄清楚是什么。当用户输入一个较低的 ' taxable美元错了。如果有人能看一眼我的代码并帮助我,我将不胜感激。PS,因为我是在调试程序,所以只填写了“单身”和“夫妻共同备案”。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class IncomeTax2 extends JFrame implements ItemListener
{
private JTextField jtfIncome;
private JTextField jtfTax;
private JButton jbCalculate;
private JComboBox jcb;
private JLabel info;
private String[] fsChoice =
{
"", "Single", "Married filing jointly", "Married filing separately",
"Head of Household"
};
int income;
double tax;
private String filingStatus;
public static void main(String[] args)
{
JFrame frame = new IncomeTax2();
frame.pack();
frame.setTitle("Income Tax Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public IncomeTax2()
{
JPanel p = new JPanel();
p.setLayout(new GridLayout(3, 2, 5, 5));
p.setBorder(new EmptyBorder(5, 5, 5, 5));
p.add(jtfIncome = new JTextField(8));
p.add(jcb = new JComboBox(fsChoice));
p.add(info = new JLabel("Enter taxable income "
+ "than pick filing status"));
p.add(jbCalculate = new JButton("Calculate"));
p.add(jtfTax = new JTextField(16));
jtfTax.setEditable(false);
setLayout(new BorderLayout());
jtfTax.setHorizontalAlignment(JTextField.RIGHT);
add(p, BorderLayout.CENTER);
jtfIncome.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
if (!(Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE)
|| c == KeyEvent.VK_DELETE || c == KeyEvent.VK_ENTER))
{
Toolkit.getDefaultToolkit().beep();
System.out.println("User must enter a digit.");
e.consume();
}
}
});
jcb.addItemListener(this);
jbCalculate.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
calculate();
}
});
}
public void itemStateChanged(ItemEvent e)
{
income = Integer.parseInt(jtfIncome.getText());
if (e.getStateChange() == ItemEvent.SELECTED)
{
filingStatus = (String) jcb.getSelectedItem();
System.out.println(filingStatus);
switch (filingStatus)
{
case "Single":
if (income <= 8350)
{
tax = income * 0.10;
}
if (income >= 8351 && income <= 33950)
{
tax = income * 0.15;
}
if (income >= 33951 && income <= 82250)
{
tax = income * 0.25;
}
if (income >= 82251 && income <= 171550)
{
tax = income * 0.28;
}
if (income >= 171551 && income <= 372950)
{
tax = income * 0.33;
} else
{
tax = income * 0.35;
}
break;
case "Married filing jointly":
if (income <= 16700)
{
tax = income * 0.10;
}
if (income >= 16701 && income <= 67900)
{
tax = income * 0.15;
}
if (income >= 67901 && income <= 137050)
{
tax = income * 0.25;
}
if (income >= 137051 && income <= 208850)
{
tax = income * 0.28;
}
if (income >= 208851 && income <= 372950)
{
tax = income * 0.33;
} else
{
tax = income * 0.35;
}
break;
}
}
}
public void calculate()
{
jtfTax.setText((int) (tax - 2000) + "");
}
}