Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于阵列的纳税人信息存储_Java - Fatal编程技术网

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());
}