Java 是否可以执行两种不同的“操作”+=&引用;及-=&引用;一行代码中的操作?

Java 是否可以执行两种不同的“操作”+=&引用;及-=&引用;一行代码中的操作?,java,math,operation,Java,Math,Operation,我刚刚开始自学java,我想知道是否有一个操作(可能是“and then”操作)允许我在同一行上执行两个数学计算。保持“余额”的最新总额很重要。这是我的代码: public static void main(String[] args) { double balance = 5400d; double withdrawalAmount = 2100d; double depositAmount = 1100d; //Total Balance after primary tra

我刚刚开始自学java,我想知道是否有一个操作(可能是“and then”操作)允许我在同一行上执行两个数学计算。保持“余额”的最新总额很重要。这是我的代码:

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);
  }

没有Java语法可以做到这一点,但您仍然可以使用简单的数学来做到这一点

您要执行以下操作:

X = X  - y + z

这里不需要两个作业。您只需减去一个值并添加另一个值,然后再将单个赋值返回到X。

您只需在一行中完成:

System.out.println(balance = balance - withdrawalAmount + depositAmount);

为什么不直接执行
System.out.println(余额=余额-取款金额+存款金额)?谢谢你们,这比我希望的还要好。不幸的是,我不确定谁先回答,没有答案比另一个好,所以我给了他们所有人一张赞成票,并接受了我在网上看到的第一个答案。是的,我想我确实比其他人领先了几秒钟:。