java - 是否可以在一行代码中执行两种不同的“+=”和“-=”操作?
问题描述
我刚刚开始自学java,我想知道是否有一个操作(可能是一个“然后”操作)可以让我在同一行上执行两个数学计算。保持“余额”的更新总数很重要。这是我的代码:
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 进行一次赋值之前,您只需减去一个值并添加另一个值。
推荐阅读
- javascript - 想要从我单击它的按钮获取先前的输入
- sql-server - T-SQL 从数字中获取一周的第一天
- c++ - 使用鼠标事件计算相机的位置和旋转
- excel - 尝试使用 FileSystemObject 打开 csv 时出错:代码运行但什么也不做
- html - X 和 Y 轴上的 HTML 表格滚动功能
- java - 如何在 AKKA 的有限状态机中正确传递数据
- mysql - MySQL:SQL JOIN、COUNT 和 WHERE 语句
- python - 熊猫按标签查找内容不起作用
- playframework - Play 最多只能支持 100 个打开的连接
- css - Qt:根据状态更改字体:启用:禁用 CSS