帐户余额= 107000.当用户输入106500时,它将给出else语句。任何想法如何解决这个问题?
while (menuChoice == 1) {
System.out.println("How much do you want to transfer?");
transferAmount = console.nextDouble(); // Getting transfer amount
// from user input
System.out.println("Who do you want to transfer this to?");
transferName = console.next(); // Getting a transfer name from user input
accountBalance = (accountBalance - transferAmount); // The calculation for account balance
if (accountBalance > transferAmount) { // Only allowing transfer if the transfer amount is less than the account balance
System.out.println(""); // spacing out the output
System.out.println("Transfer Complete");
outfile.println("The balance for the port account is £"+ accountBalance + " You have sent £"+ transferAmount + " to " + transferName+ " on the following date and time, " + Date); // Printing line to output file about the transaction details
outfile.close();
menuChoice = 0; // Ending the while loop if true
} else // Denying a transfer if the transfer amount is more than the account balance
System.out.println("Warning You will go overdrawn!");
menuChoice = 0;
}
答案 0 :(得分:1)
您在决定是否安全之前从余额中减去转帐金额:
accountBalance = (accountBalance - transferAmount);
这应该在您确定有足够的资金转移后才会发生。
答案 1 :(得分:0)
if accountbalance> transferamount然后你必须减去accountBalance =(accountBalance - transferAmount); 在if语句中执行此操作。