在我的程序中,我将CAD转换为USD作为示例,当我放入$ 10 CAD时,我得到$ 9.14 USD返回,这是正确的。但在此之后,例如,我添加50美元CAD并将其转换,它将显示$ 60 CAD是$ 54.86 USD,因为它应该只用50美元而不是60美元转换。这是因为arraylist中的数字正在增加,并且不知道该怎么办?防止ArrayList添加
下面是图片示例
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
double moneyValue = Double.parseDouble(moneyInput.getText());
int type = Integer.parseInt(typeInput.getText());
double conversion;
initialMoney.add(moneyValue);
if (moneyValue < 0){
errorLabel.setText("Value added is negative, please try again.");
}
else if(type < 1 || type > 20){
errorLabel.setText("An error has occurred, please try again.");
}
else if (moneyValue > 0){
if (type == 1){
conversion = initialCADtofinalUSD(moneyValue);
finalMoney.add(conversion);
}
else if (type == 2){
conversion = initialCADtofinalEUR(moneyValue);
finalMoney.add(conversion);
}
else if (type == 3){
conversion = initialCADtofinalCNY(moneyValue);
finalMoney.add(conversion);
}
else if (type == 4){
conversion = initialCADtofinalGBP(moneyValue);
finalMoney.add(conversion);
}
这里是它输出
for (int i = 0; i < initialMoney.size(); i++){
initialMoneyOutput += initialMoney.get(i);
finalMoneyOutput += finalMoney.get(i);
}
DecimalFormat x = new DecimalFormat("0.00");
textAreaOutput.append("$" + x.format(initialMoneyOutput) + initialCurrencyAcronym + " is $" + x.format(finalMoneyOutput) + finalCurrencyAcronym + "\n");
如何向我们展示你的代码? – Mureinik
您将第二个值添加到第一个*代码中的某处*。你如何期待我们解决它而不提出它? –
你需要为我们分享一些代码来弄清楚什么是错误的,但是没有看到任何东西,我的猜测是你正在重新使用一个变量(保存要转换的数量)并以某种方式添加它。 –