[英]How to fix if and else if statements not choosing the right statement
Im doing a Box calculator that takes the input of the length width and height and then calculates it and compares the two and says something like "The first box is slightly bigger then the next one" vice versa. 我正在做一个Box计算器,它接受长度,宽度和高度的输入,然后对其进行计算,然后将两者进行比较,然后说出类似“第一个盒子稍大然后下一个盒子”的意思,反之亦然。 The issue is I have multiple statements that say if the box is double triple quadruple the size but it just sees that box one is greater then box two and display it.
问题是我有多个语句说如果框是大小的两倍,三倍,四倍,但它只是看到框一比框二大,然后显示它。
I have tried all if statements dividing the box or multiplying 我已经尝试了所有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");
}
}
You need to reverse the order of the if
s . 您需要颠倒
if
s的顺序。 First you need to check the amount of 首先,您需要检查
firstBox.calcVolume() / secondBox.calcVolume()
and secondBox.calcVolume() / firstBox.calcVolume()
and if they are bigger than 4 use those if
s else use the first set of if
s in reverse order. firstBox.calcVolume() / secondBox.calcVolume()
和secondBox.calcVolume() / firstBox.calcVolume()
并且如果它们大于4使用的那些if
小号否则使用第一组if
S IN相反的顺序。
The problem with your code is the first conditions are already true when your last conditions are, so it will never go to them. 您的代码的问题是,当您的最后一个条件满足时,第一个条件已经为真,因此永远不会使用它们。 I mean when
a
number is four times bigger than number b
, it is also bigger than it :), so upper messages will be set. 我的意思是,当
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.