我正在做一个盒子计算器,它输入长度、宽度和高度,然后计算它并比较两者,然后说“第一个盒子比下一个盒子稍大”,反之亦然。问题是我有多个语句说盒子的大小是否是双三倍四倍,但它只是看到盒子一大于盒子二并显示它。
我已经尝试了所有除框或乘法的 if 语句
public void calculateSizes() {
firstBox.calcVolume();
secondBox.calcVolume();
if (firstBox.calcVolume() == secondBox.calcVolume()) { //Calculate the size difference and display the corresponding message
message = ("The first box is the same size as the second box");
}else if (firstBox.calcVolume() > secondBox.calcVolume() ) {
message = ("The first box is slightly bigger than the second box");
}else if (secondBox.calcVolume() > firstBox.calcVolume()) {
message = ("The second box is slightly bigger than the first box");
}else if (firstBox.calcVolume() >= secondBox.calcVolume() / 2) {
message = ("The first box is twice the size than the second box");
}else if (secondBox.calcVolume() >= firstBox.calcVolume() / 2) {
message = ("The second box is twice the size than the first box");
}else if (firstBox.calcVolume() >= secondBox.calcVolume() / 3) {
message = ("The first box is triple the size than the second box");
}else if (secondBox.calcVolume() >= firstBox.calcVolume() / 3) {
message = ("The second box is triple the size than the first box");
}else if (firstBox.calcVolume() >= secondBox.calcVolume() / 4) {
message = ("The first box is quadruple the size than the second box");
}else if (secondBox.calcVolume() >= firstBox.calcVolume() / 4) {
message = ("The second box is quadruple the size than the first box");
}else if (firstBox.calcVolume() == firstBox.calcVolume() / secondBox.calcVolume()) {
message =("\n Box one is " + firstBox.calcVolume() / secondBox.calcVolume() + " times the size second box" );
}else if (secondBox.calcVolume() == secondBox.calcVolume() / firstBox.calcVolume()) {
message =("\n Box two is " + secondBox.calcVolume() / firstBox.calcVolume() + " times the size first box");
}
}
最佳答案
您需要颠倒 if
的顺序。首先您需要检查
的金额
firstBox.calcVolume()/secondaryBox.calcVolume()
和 secondBox.calcVolume()/firstBox.calcVolume()
,如果它们大于 4,则使用这些 if
,否则以相反的顺序使用第一组 if
。
您的代码的问题是,当您的最后一个条件成立时,第一个条件已经成立,因此它永远不会到达它们。我的意思是,当 a
数字比数字 b
大四倍时,它也比它大:),因此将设置上方消息。
public void calculateSizes() {
//if you assign these values to variables it will be much better and you wont need to compute it all the time
firstBox.calcVolume();
secondBox.calcVolume();
message = "\n ";
if (secondBox.calcVolume() > firstBox.calcVolume() && secondBox.calcVolume() / firstBox.calcVolume() > 4 ) {
message += ("Box two is " + (secondBox.calcVolume() / firstBox.calcVolume()) + " times the size first box");
} else if (firstBox.calcVolume() > secondBox.calcVolume() && firstBox.calcVolume() / secondBox.calcVolume() > 4 ) {
message += ("Box one is " + (firstBox.calcVolume() / secondBox.calcVolume()) + " times the size second box" );
} else if (firstBox.calcVolume() == secondBox.calcVolume()) {
message += ("The first box is the same size as the second box");
} else if (secondBox.calcVolume() >= firstBox.calcVolume() / 4) {
message += ("The second box is quadruple the size than the first box");
} else if (firstBox.calcVolume() >= secondBox.calcVolume() / 4) {
message += ("The first box is quadruple the size than the second box");
} else if (firstBox.calcVolume() >= secondBox.calcVolume() / 3) {
message += ("The first box is triple the size than the second box");
} else if (secondBox.calcVolume() >= firstBox.calcVolume() / 3) {
message += ("The second box is triple the size than the first box");
} else if (firstBox.calcVolume() >= secondBox.calcVolume() / 2) {
message += ("The first box is twice the size than the second box");
} else if (secondBox.calcVolume() >= firstBox.calcVolume() / 2) {
message += ("The second box is twice the size than the first box");
} else if (firstBox.calcVolume() > secondBox.calcVolume() ) {
message += ("The first box is slightly bigger than the second box");
}
}
关于java - 如何修复 if 和 else if 语句未选择正确的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55319902/