[英]How to fix if and else if statements not choosing the right statement
我正在做一個Box計算器,它接受長度,寬度和高度的輸入,然后對其進行計算,然后將兩者進行比較,然后說出類似“第一個盒子稍大然后下一個盒子”的意思,反之亦然。 問題是我有多個語句說如果框是大小的兩倍,三倍,四倍,但它只是看到框一比框二大,然后顯示它。
我已經嘗試了所有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
s的順序。 首先,您需要檢查
firstBox.calcVolume() / secondBox.calcVolume()
和secondBox.calcVolume() / firstBox.calcVolume()
並且如果它們大於4使用的那些if
小號否則使用第一組if
S IN相反的順序。
您的代碼的問題是,當您的最后一個條件滿足時,第一個條件已經為真,因此永遠不會使用它們。 我的意思是,當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.