Java 如何修复if和elseif语句没有选择正确的语句
我在做一个长方体计算器,输入长度、宽度和高度,然后计算并比较两者,然后说“第一个长方体比下一个稍大”,反之亦然。问题是,我有多条语句,说如果盒子是大小的两倍,三倍,四倍,但它只看到盒子一比盒子二大,并显示它 我已经尝试了所有的if语句,将方框分割或相乘Java 如何修复if和elseif语句没有选择正确的语句,java,if-statement,Java,If Statement,我在做一个长方体计算器,输入长度、宽度和高度,然后计算并比较两者,然后说“第一个长方体比下一个稍大”,反之亦然。问题是,我有多条语句,说如果盒子是大小的两倍,三倍,四倍,但它只看到盒子一比盒子二大,并显示它 我已经尝试了所有的if语句,将方框分割或相乘 public void calculateSizes() { firstBox.calcVolume(); secondBox.calcVolume(); if (firstBox.calcVolume() == sec
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");
}
}
如果s,则需要颠倒
的顺序。首先,您需要检查
firstBox.calcVolume()/secondBox.calcVolume()
和secondBox.calcVolume()/firstBox.calcVolume()
如果大于4,则使用这些if
s,否则按相反顺序使用第一组if
s
代码的问题是,当最后一个条件为真时,第一个条件已经为真,因此它永远不会进入它们。我的意思是当a
number比number
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");
}
}
如果s,则需要颠倒
的顺序。首先,您需要检查
firstBox.calcVolume()/secondBox.calcVolume()
和secondBox.calcVolume()/firstBox.calcVolume()
如果大于4,则使用这些if
s,否则按相反顺序使用第一组if
s
代码的问题是,当最后一个条件为真时,第一个条件已经为真,因此它永远不会进入它们。我的意思是当a
number比number
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");
}
}
刚刚编辑了我的答案,请运行它,看看是否有效,并指定firstBox.calcVolume();另一行类似于
int firstVolume=firstBox.calcVolume()例如,如果该方法返回一个int。刚刚编辑了我的答案,请运行它,看看是否有效,并请分配firstBox.calcVolume();另一行类似于int firstVolume=firstBox.calcVolume()例如,如果该方法返回int,则返回code>。