我有这个while循环,当用户输入费用大于交易金额时运行。例如,如果费用为4美元而您只需支付2美元,则会说您还欠2美元并提示您输入更多付款。问题是,如果付款仍然很短,我无法弄清楚如何在付款后更新变量交易。在它要求另一笔付款之后,说你仍然缺少4美元并再支付1美元,这将给你总共3美元,该计划应该说你仍然短缺1美元。尽管如此,该计划仍然表示您的原始金额很短,即2美元。
while (transaction < feeSum)
{
double underPay = feeSum - transaction;
System.out.println("The transaction did not meet the fee by $" + underPay);
System.out.println("Please enter another payment to complete the balance.");
System.out.println("Enter a number of payments.");
int paymentSize2 = keyboard.nextInt();
double[] payments2 = new double[paymentSize2];
System.out.println("Enter " + payments2.length + " payment(s).");
double paymentSum2 = 0;
for(int i = 0; i < payments2.length; i++)
{
payments2[i] = keyboard.nextDouble();
paymentSum2 = paymentSum2 + payments[i];
transaction += paymentSum2; //<<<<<<< Shouldn't this update transaction?
} // The second time around it should say the trans did not meet fee by $1
if (paymentSum2 == underPay)
{
System.out.println("There is now no outstanding balance.");
break;
}
答案 0 :(得分:0)
我重新编写了一些代码,这可行,但它并不包含您实现的所有额外内容。我真的不明白为什么你需要这些数组。 如果您支付的费用超过了您的预期,则此代码无法处理,但这可以在while循环的最后几行中轻松实现。
double underPay = feeSum - transaction;
while (underPay != 0) {
System.out.println("The transaction did not meet the fee by $" + underPay);
System.out.println("Please enter another payment to complete the balance.");
int paymentSizeNext = keyboard.nextInt();
underPay -= paymentSizeNext;
}
System.out.println("There is now no outstanding balance.");