Java 基于阵列的纳税人信息存储
那么,如果我在Java 基于阵列的纳税人信息存储,java,Java,那么,如果我在do while循环之前,询问“x”税额初始化子项和grossIncomes数组,该怎么办呢 扩展do while循环,直到JOptionPane.showMessageDialog(null,message) 不要使用numTaxpayers作为子项和grossIncomes的索引。在循环开始之前,使用count,并将count初始化为0 int[] children = new int[numTaxpayers]; double[
do while
循环之前,询问“x”税额初始化子项和grossIncomes
数组,该怎么办呢
扩展do while循环,直到JOptionPane.showMessageDialog(null,message)代码>
不要使用numTaxpayers
作为子项和grossIncomes
的索引。在循环开始之前,使用count
,并将count初始化为0
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);
}
int[]children=newint[numTaxpayers];
double[]grossIncomes=新的double[numTaxpayers];
整数计数=0;
while(计数
您正在重新分配循环中的numChildren
和grossIncome
变量,而不是存储它们:
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 List<TaxPayer> taxpayers = new LinkedList<TaxPayer>();
for(int i = 0; i < numTaxpayers; ++i) {
taxpayers.add(TaxPayer.requestTaxPayerDetails());
}
final List=new LinkedList();
对于(int i=0;i
稍微整洁一点,不?这是正确的,但是一旦扩展了循环,就应该在while循环之前初始化数组(double[]grossIncomes
和int[]children
)。然后是while循环,使用count
而不是numTaxpayers
。所以不要使用do-while循环?使用什么都不重要。您需要保留读取的值。您正在重新分配每次迭代的值。在任何情况下,for
循环都要简单得多。
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());
}