那么如果我要问“x”税额怎么办
问问题
83 次
2 回答
2
在循环之前初始化children
andgrossIncomes
数组。do-while
延长do-while循环直到JOptionPane.showMessageDialog(null,message);
不要用作和numTaxpayers
的索引。在循环开始之前使用,并将 count 初始化为 0。children
grossIncomes
count
int[] children = new int[numTaxpayers];
double[] grossIncomes = new double[numTaxpayers];
int count = 0;
while (count < numTaxpayers)
{
int numChildren = Integer.parseInt(JOptionPane.showInputDialog("How many children do you have?"));
double grossIncome = Double.parseDouble(JOptionPane.showInputDialog("What is your gross income?"));
children[count] = numChildren;
grossIncomes[count] = grossIncome;
count ++;
// Calculations goes here
//....
//...
JOptionPane.showMessageDialog(null,message);
}
于 2013-10-24T19:42:31.407 回答
0
您正在循环中重新分配numChildren
andgrossIncome
变量,而不是存储它们:
do
{
numChildren = Integer.parseInt(JOptionPane.showInputDialog("How many children do you have?"));
grossIncome = Double.parseDouble(JOptionPane.showInputDialog("What is your gross income?"));
count ++;
} while (count <= numTaxpayers);
应该
final int[] children = new int[numTaxpayers];
final double[] grossIncomes = new double[numTaxpayers];
for(int i = 0; i < numTaxpayers; ++i) {
children[i] = Integer.parseInt(JOptionPane.showInputDialog("How many children do you have?"));
grossIncomes[i] = Double.parseDouble(JOptionPane.showInputDialog("What is your gross income?"));
}
因此,您创建数组,然后为每个纳税人将其数组元素分配给查询结果。
我会进一步建议您将您的封装TaxPayer
为一个对象,并将与它们相关的方法保留在该对象中。
public class TaxPayer {
private final int numChildren;
private final int grossIncome;
private TaxPayer(final int numChildren, final int grossIncome) {
this.numChildren = numChildren;
this.grossIncome = grossIncome;
}
public static TaxPayer requestTaxPayerDetails() {
final int numChildren = Integer.parseInt(JOptionPane.showInputDialog("How many children do you have?"));
final int grossIncome = Double.parseDouble(JOptionPane.showInputDialog("What is your gross income?"));
return new TaxPayer(numChildren, grossIncome);
}
public int findTaxDependency() {
// do stuff
}
public double calculateTax() {
// do stuff
}
}
接着
final List<TaxPayer> taxpayers = new LinkedList<TaxPayer>();
for(int i = 0; i < numTaxpayers; ++i) {
taxpayers.add(TaxPayer.requestTaxPayerDetails());
}
更整洁一点,不是吗?
于 2013-10-24T19:49:37.080 回答