Reputation: 101
I have just started teaching myself java and I was wondering if there is an operation (maybe an "and then" operation) that will allow me to perform two mathematical calculations on the same line. It is important to keep the updated total for the "balance". This is my code:
public static void main(String[] args) {
double balance = 5400d;
double withdrawalAmount = 2100d;
double depositAmount = 1100d;
//Total Balance after primary transactions
// (This line of code works but I want to update "balance" after every calculation
System.out.println(balance - withdrawalAmount + depositAmount);
//This updates the balance after withdrawing money
System.out.println(balance -= withdrawalAmount);
//This updates balance after depositing money
System.out.println(balance += depositAmount);
//Here is where I was trying to combine both operations but it did not like this very much
System.out.println(balance -= withdrawalAmount && balance += depositAmount);
}
Upvotes: 3
Views: 462
Reputation: 3866
You can simply do it in one line:
System.out.println(balance = balance - withdrawalAmount + depositAmount);
Upvotes: 4
Reputation: 140427
There is no Java syntax to do that, but you can still do it, using plain simple math.
You want to do:
X = X - y + z
You don't need two assignments here. You simply subtract one value and add another one before you do a single assignment back to X.
Upvotes: 10