java - 为什么我的 if(else if) 语句被忽略?
问题描述
public void stackPower(int stackAmount)
{
super.stackPower(stackAmount);
if (this.amount == -1) {
this.amount = -2;
}
else if (this.amount == -2) {
this.amount = -3;
}
if (this.amount == -3) {
this.amount = -4;
}
}
在测试期间,值从 -1 到 -2 到 -4 到 -6 等。
我想要发生的事情:从 -1 到 -2 到 -3 到 -4 然后停止。
有人可以解释我在这里缺少什么以及如何解决我的问题吗?谢谢。
解决方案
您的第三个if
条件缺少一个else
(但也可以很容易地成为一个else
块)。喜欢,
if (this.amount == -1) {
this.amount = -2;
} else if (this.amount == -2) {
this.amount = -3;
} else {
this.amount = -4;
}
但是,我会通过调用like来简化逻辑Math.max(int, int)
this.amount = Math.max(-4, this.amount - 1);
推荐阅读
- node-modules - Botpress 用户界面 - 导出机器人
- canvas - 三.js 在图片加载前去除背景
- python - Django 错误:“RelatedManager”对象没有属性“保存”
- javascript - Vue.js - 禁用页面的 removeAttributeQuotes
- api - 通过 AoG 中的 OAuth 获取有关 Actions on Google 的用户信息?
- asp.net - 如何在asp.net的gridview内的asp boundfield datafield hedertext上设置工具提示?
- vb.net - Renaming the current process of a ClickOnce application programmatically
- html - -webkit-line-clamp css not able to add in email template. How can add this property in email template?
- git - 当仅从先前版本修改补丁描述时,如何将补丁系列提交到 Linux 邮件列表并更改版本?
- sorting - how to customized sorting in an enum class in kotlin