那么,如果我要问的税“X”量纳税人信息存储使用数组
0
A
回答
2
初始化do-while
循环之前的children
和grossIncomes
阵列。
延长do-while循环,直到JOptionPane.showMessageDialog(null,message);
不要使用numTaxpayers
为children
和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);
}
0
您重新分配在循环的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 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());
}
有点整洁,不是吗?
+0
那么不要使用do-while循环? – Dan
+0
你使用什么并不重要。你需要保持你阅读的价值。您正在重新分配每个迭代的值。无论如何,'for'循环要简单得多。 –
相关问题
- 1. 数据库:存储税率和信息
- 2. 存储过程计算应纳税额
- 3. 使用归纳时保存信息?
- 4. 如何将消费税信息存储在数据库中
- 5. 使用数组来存储信息和用PHP
- 6. 如何存储联系人信息
- 7. 存储个人信息的好方法
- 8. 存储销售税信息的最佳方式
- 9. 存储信息()
- 10. 存储信息
- 11. 存储应用信息
- 12. Magento订单税务信息
- 13. Woocommerce纳税问题
- 14. IPP应纳税额
- 15. Azure存储信息
- 16. Java - 存储信息
- 17. 存储卡信息
- 18. C#存储信息
- 19. 使用DynamicObject存储类型信息
- 20. 使用按钮存储附加信息
- 21. 使用FormsAuthentication.SetAuthCookie存储更多信息
- 22. 使用web.config存储和映射信息
- 23. 使用信息存储列表
- 24. scrapy使用mongodb存储信息
- 25. 本地存储信息不使用NSUserDefaults
- 26. 使用ArrayList存储信息并访问信息
- 27. 转换程序将信息存储在数组中并使用组合框
- 28. 存储保存的信息
- 29. 替代存储信用卡信息
- 30. 第三方存储信用卡信息?
这是正确的,但是一旦你扩展了循环,你应该在while循环之前初始化数组(double [] grossIncomes'和int [] children')。然后是while循环,并使用'count'而不是'numTaxpayers'。 –