Is it possible to perform two different "+=" and "-=" operations in one line of code?

Kevin Ocampo :

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);
  }
GhostCat salutes Monica C. :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=84554&siteId=1