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,然后停止。
谁能解释一下我在这里遗漏了什么,以及如何修复我的问题?谢谢。
发布于 2018-12-16 10:46:04
您的第三个if条件缺少一个else (但也可能是一个else块)。喜欢,
if (this.amount == -1) {
this.amount = -2;
} else if (this.amount == -2) {
this.amount = -3;
} else {
this.amount = -4;
}但是,我可以通过调用Math.max(int, int)来简化逻辑,比如
this.amount = Math.max(-4, this.amount - 1);发布于 2018-12-16 10:55:42
if (this.amount == -1) {
this.amount = -2;
}
else if (this.amount == -2) {
this.amount = -3;
}我认为else if子句永远不会执行,因为一旦执行了if子句,else子句就永远不会执行。
发布于 2018-12-16 12:12:11
只需将else if更改为if就可以解决这个问题。
你遇到的问题是,当你有一个if -语句和一个else-if时,如果我们进入if-语句,我们会跳过后续的else-if语句或else语句。
public void stackPower(int stackAmount)
{
super.stackPower(stackAmount);
if (this.amount == -1) {
this.amount = -2;
}
if (this.amount == -2) {
this.amount = -3;
}
if (this.amount == -3) {
this.amount = -4;
}
}https://stackoverflow.com/questions/53799011
复制相似问题